-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpat1076.cpp
80 lines (77 loc) · 1.29 KB
/
pat1076.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <vector>
#include <queue>
#include <memory.h>
using namespace std;
vector<int> edge[1010];
struct E{
int id;
int level;
};
queue<E> q;
bool mark[1010];
void init(){
for(int i=0;i<1010;i++)
edge[i].clear();
return;
}
void bfsInit(){
memset(mark,0,1010*sizeof(bool));
while(!q.empty())
q.pop();
}
int bfs(int start,int l){
bfsInit();
int ret=0;
E tmp;
mark[start]=true;
for(int i=0;i<edge[start].size();i++){
tmp.id=edge[start][i];
tmp.level=1;
q.push(tmp);
mark[tmp.id]=true;
ret++;
}
while(!q.empty()){
tmp=q.front();
q.pop();
int nowId=tmp.id;
int nowL=tmp.level;
if(nowL==l)
break;
for(int i=0;i<edge[nowId].size();i++){
tmp.id=edge[nowId][i];
tmp.level=nowL+1;
if(!mark[tmp.id]){
q.push(tmp);
ret++;
mark[tmp.id]=true;
}
}
}
return ret;
}
int main(){
int n,l;
while(scanf("%d%d",&n,&l)!=EOF){
init();
for(int i=1;i<=n;i++){
int size;
scanf("%d",&size);
for(int j=0;j<size;j++){
int f;
scanf("%d",&f);
edge[f].push_back(i);
}
}
int m;
scanf("%d",&m);
for(int i=0;i<m;i++){
int start;
scanf("%d",&start);
int ans=bfs(start,l);
printf("%d\n",ans);
}
}
return 0;
}