-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14-4.cpp
61 lines (55 loc) · 987 Bytes
/
14-4.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
#include <iostream>
#include <vector>
using namespace std;
class Graph {
public:
int size;
vector<int>* v;
bool* visit;
Graph(int size) {
this->size = size;
v = new vector<int>[size + 1];
visit = new bool[size + 1];
for (int i = 0; i < size + 1; i++) {
visit[i] = false;
}
}
void insertEdge(int vertex1, int vertex2) {
v[vertex1].push_back(vertex2);
v[vertex2].push_back(vertex1);
}
int dfs(int cur) {
int size = 0;
visit[cur] = true;
size++;
for (int i = 0; i < v[cur].size(); i++) {
int next = v[cur][i];
if (!visit[next]) {
size += dfs(next);
}
}
return size;
}
void reset() {
for (int i = 0; i < size + 1; i++) {
visit[i] = false;
}
}
};
int main() {
int t, n, m, a, v1, v2;
cin >> t;
while (t--) {
cin >> n >> m >> a;
Graph g(n);
for (int i = 0; i < m; i++) {
cin >> v1 >> v2;
g.insertEdge(v1, v2);
}
for (int i = 0; i < a; i++) {
cin >> v1;
cout << g.dfs(v1) << endl;
g.reset();
}
}
}