-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathA1076.cpp
56 lines (53 loc) · 1023 Bytes
/
A1076.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
const int MAXN = 1000 + 10;
int N, L;
struct Node{
int v, level;
Node(int a, int b = 0): v(a),level(b){}
};
vector<Node> Adj[MAXN];
bool inq[MAXN] = {false};
int BFS(int s){
int numForward = 0;
queue<Node> q;
q.push(Node(s, 0));
inq[s] = true;
while(!q.empty()){
Node front = q.front();
q.pop();
int v = front.v;
for(int i = 0; i < Adj[v].size(); ++i){
Node next = Adj[v][i];
next.level = front.level + 1;
if(inq[next.v] == false && next.level <= L){
q.push(next);
inq[next.v] = true;
++numForward;
}
}
}
return numForward;
}
int main(){
scanf("%d%d", &N, &L);
for(int i = 1; i <= N; ++i){
int k, follower;
scanf("%d", &k);
for(int j = 0; j < k; ++j){
scanf("%d", &follower);
Adj[follower].push_back(Node(i));
}
}
int K, s;
scanf("%d", &K);
for(int i = 0; i < K; ++i){
memset(inq, false, sizeof(inq));
scanf("%d", &s);
printf("%d\n", BFS(s));
}
return 0;
}