-
Notifications
You must be signed in to change notification settings - Fork 0
/
1013.cpp
69 lines (51 loc) · 1.27 KB
/
1013.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
#include<iostream>
#include<vector>
using namespace std;
typedef struct {
bool isVisited;
vector<int> to;
} City;
City citys[1000];
void init(int city, bool isvisited) {
citys[city].isVisited = isvisited;
}
void initAll(int n) {
for(int i = 1; i <= n; i++) {
init(i, false);
}
}
void linkCity(int acity, int bcity) {
citys[acity].to.push_back(bcity);
citys[bcity].to.push_back(acity);
}
void findLinkedCity(int city) {
citys[city].isVisited = true;
for(auto linkCity: citys[city].to) {
if(!citys[linkCity].isVisited)
findLinkedCity(linkCity);
}
}
int main() {
int cityNum, roadNum, checkNum;
cin >> cityNum >> roadNum >> checkNum;
for(int road = 0; road < roadNum; road++) {
int aCity, bCity;
cin >> aCity >> bCity;
linkCity(aCity, bCity);
}
for(int check = 0; check < checkNum; check++) {
int cityTocheck, count = 0;
cin >> cityTocheck;
initAll(cityNum);
init(cityTocheck, true);
for(int city = 1; city <= cityNum; city++) {
if(!citys[city].isVisited) {
count++;
findLinkedCity(city);
}
}
if(check) cout << endl;
cout << count - 1;
}
return 0;
}