-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathA1076.cpp
More file actions
65 lines (56 loc) · 1.39 KB
/
Copy pathA1076.cpp
File metadata and controls
65 lines (56 loc) · 1.39 KB
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
57
58
59
60
61
62
63
64
65
#include <cstdio>
#include <queue>
#include <cstring>
#define MAXN 1005
using namespace std;
int n, l, k;
bool edge[MAXN][MAXN] = {}; // “edge[a][b] = true”:path a -> b exists
int layer[MAXN] = {}; // “layer[v] = x”:the layer of vertex, v is x
int in_queue[MAXN] = {};
int bfs(int id)
{
int possible_forward = 0;
queue<int> q;
q.push(id);
in_queue[id] = true;
layer[id] = 0;
while (q.size())
{
int front = q.front(), front_layer = layer[front];
q.pop();
if (1 <= front_layer && front_layer <= l)
possible_forward++;
for (int i = 1; i <= n; i++)
if (in_queue[i] == false && edge[front][i] == true)
{
q.push(i);
in_queue[i] = true;
layer[i] = front_layer + 1;
}
}
return possible_forward;
}
int main()
{
scanf("%d %d", &n, &l);
for (int follower = 1; follower <= n; follower++)
{
int m, followee;
scanf("%d", &m);
for (int j = 1; j <= m; j++)
{
scanf("%d", &followee);
edge[followee][follower] = true;
}
}
scanf("%d", &k);
for (int query = 0; query < k; query++)
{
int id;
scanf("%d", &id);
printf("%d\n", bfs(id));
memset(layer, 0, sizeof(layer));
memset(in_queue, 0, sizeof(in_queue));
}
return 0;
}