1076. Forwards on Weibo

#include<iostream>
#include<algorithm>
#include<queue>
#include<deque>
using namespace std;
deque<deque<int> >ed;
int l,n,m;
int post(int k)
{
    int cnt=0;
    vector<bool>used(n+1);
    vector<int>deep(n+1);
    queue<int>que;
    que.push(k);
    used[k]=true;
    while(que.size())
    {
        int t=que.front();
        que.pop();
        for(int son,i=0;i<ed[t].size();++i)
            if(!used[son=ed[t][i]])
            {
                used[son]=true;
                deep[son]=deep[t]+1;
                if(deep[son]>l) continue;
                ++cnt;
                que.push(son);
            }
    }
    return cnt;
}
int main()
{
    cin>>n>>l;
    ed.resize(1+n);
    for(int p,k,i=1;i<=n;++i)
    {
        cin>>k;
        while(k--)
        {
            cin>>p;
            ed[p].push_back(i);
        }
    }
    cin>>m;
    while(m--)
    {
        int k;
        cin>>k;
        cout<<post(k)<<endl;
    }
    return 0;
}

你可能感兴趣的:(1076. Forwards on Weibo)