1076. Forwards on Weibo (30) 考察层序遍历
#include <iostream>#include <vector>#include <algorithm>using namespace std;int main(){ int n,level; vector<vector<int>> v(2000); cin>>n>>level; for(int i=1;i<=n;++i) { int k,u; cin>>k; while(k--) { cin>>u; v[u].push_back(i); } } int query; cin>>query; while(query--) { int cur; cin>>cur; int queue[2000],front=0,rear=0; int cnt=1,visit[2000]={0},curlevel=0,first=0,last=1; queue[rear++]=cur; visit[cur]=1; while(front!=rear) { int s=queue[front++]; ++first; for(auto it=v[s].begin();it!=v[s].end();++it) { if(visit[*it]==0){ queue[rear++]=*it; visit[*it]=1;++cnt; } } if(first==last) { ++curlevel; last=cnt; } if(curlevel==level)break; } cout<<cnt-1<<endl; } return 0;}新闻热点
疑难解答