-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1013.cpp
52 lines (51 loc) · 904 Bytes
/
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
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000;
int n, m, k, uu, vv, x, ans;
vector<int> adj[MAXN];
bool vis[1000];
//在这道题中dfs和bfs性能相差不大
void dfs(int u){
vis[u] = true;
for(int i=0; i<adj[u].size(); i++){
if(vis[adj[u][i]] == false) dfs(adj[u][i]);
}
}
void bfs(int u){
queue<int> q;
q.push(u);
vis[u] = true;
while(!q.empty()){
u = q.front();
q.pop();
for(int i=0; i<adj[u].size(); i++){
if(vis[adj[u][i]] == false){
q.push(adj[u][i]);
vis[adj[u][i]] = true;
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin>>n>>m>>k;
for(int i=0; i<m; i++){
cin>>uu>>vv;
adj[uu].push_back(vv);
adj[vv].push_back(uu);
}
for(int i=0; i<k; i++){
cin>>x;
memset(vis, false, sizeof(vis));
vis[x] = true;
ans = 0;
for(int j=1; j<=n; j++){
if(vis[j] == false){
ans++;
bfs(j);
}
}
cout<<ans-1<<endl;
}
return 0;
}