https://pintia.cn/problem-sets/994805342720868352/problems/994805392092020736
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int MAXN = 1010;
struct Node {
int v;
int layer;
Node(int _v, int _l) : v(_v), layer(_l) { }
Node(int _v) : v(_v), layer(0) { }
};
vector<Node> Adj[MAXN];
vector<bool> visited(MAXN, false);
int BFS(int s, int l) {
queue<Node> que;
que.push(Node(s,0));
visited[s] = true;
int layer = 0;
int cnt = 0;
while (!que.empty()) {
int size = que.size();
layer++;
for (int i = 0; i < size; i++) {
Node tmp1 = que.front();
que.pop();
for (int j = 0; j < Adj[tmp1.v].size(); j++) {
Node tmp2 = Adj[tmp1.v][j];
if (!visited[tmp2.v]) {
tmp2.layer = layer;
que.push(tmp2);
visited[tmp2.v] = true;
cnt++;
}
}
}
if (layer == l) break;
}
return cnt;
}
void Initial() {
for (int i = 0; i < MAXN; i++) {
visited[i] = false;
}
}
int main()
{
int N, L;
cin >> N >> L;
for (int i = 1; i <= N; i++) {
int numFollow;
cin >> numFollow;
while (numFollow--) {
int idFollow;
cin >> idFollow;
Adj[idFollow].push_back(Node(i)); //idfollow ---> i
}
}
int numQuery;
cin >> numQuery;
while (numQuery--) {
Initial();
int id;
cin >> id;
int numForward = BFS(id, L);
cout << numForward << endl;
}
return 0;
}