-
Notifications
You must be signed in to change notification settings - Fork 0
/
A1076 使用结构体 结合上 DFS或者是BFS.cpp
64 lines (64 loc) · 1.25 KB
/
A1076 使用结构体 结合上 DFS或者是BFS.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
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
int n,m,l,k;
struct node{
int id;
int layer;
};
vector<vector<int> >v;
int BFS(node tnode) {
bool inq[maxn] = {false };
queue<node> q;
q.push(tnode);
//
inq[tnode.id] = true;
//计数变量
int cnt = 0;
while(!q.empty())){
node top = q.front();
q.pop();
int topid = top.id;
for(int i=0;i<v[topid].size();i++){
int nextid = v[topid][i];
if(inq[nextid] == false && top.layer < l){
node next;
next.id = nextid;
next.layer = top.layer + 1;
q.push(next);
inq[next.id] = true;
cnt++;
}
}
}
return cnt;
}
int main() {
scanf("%d %d", &n, &l);
v.resize(n + 1);
for(int i = 1; i <= n; i++) {
scanf("%d", &m);
for(int j = 0; j < m; j++) {
int temp;
scanf("%d", &temp);
//相当于是一种邻接表的感觉
v[temp].push_back(i);
}
}
scanf("%d", &k);
int tid;
for(int i = 0; i < k; i++) {
scanf("%d", &tid);
//注意这个top id 初始话的方式
node tnode;
tnode.id = tid;
tnode.layer = 0;
// ans也是一种方式之一
int ans = bfs(tnode);
printf("%d\n", ans);
}
return 0;
}