leetcde每日一题(2368. 受限条件下可到达节点的数目)

2368. 受限条件下可到达节点的数目

代码入下

class Solution {
public:
  // 建图后bfs 
int e[200010], h[200010], ne[200010], idx=0;//理解链表存储图的本质,h只需要开N,与节点数相同
// h[i] i的第一个临接点,通过e[h[i]]得到节点值,ne[h[i]]下一个临接点。
int st[200010];
void add (int a, int b)
{
    e[idx] = b,  ne[idx] = h[a], h[a] = idx ++ ;
}
    int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {
        memset(h, -1, sizeof h);
        memset(st, 0, sizeof st);
        for(int i=0;i<edges.size();i++){
            add(edges[i][1],edges[i][0]);
            add(edges[i][0],edges[i][1]);
        }
        set<int> t1;
        for(auto t:restricted) t1.insert(t);
        //bfs
        queue<int> q;
        q.push(0);
        int t3=0;
        st[0]=1;
        while(!q.empty()){
            int t2=q.front();
            q.pop();
            cout<<t2<<endl;
            if(!t1.count(t2))t3++;
            for(int i=h[t2];i!=-1;i=ne[i]){
               
                if(t1.count(e[i])){
                    continue;
                }
                if(st[e[i]]==1)continue;
                st[e[i]]=1;
                q.push(e[i]);
               
            }
        }
        return t3;
    }
};

在这里插入图片描述

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@M_J_Y@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值