-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1013.cpp
More file actions
56 lines (52 loc) · 820 Bytes
/
Copy path1013.cpp
File metadata and controls
56 lines (52 loc) · 820 Bytes
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
//求图的连通分支个数,需要修复的路就是连通分支个数-1
#include <iostream>
using namespace std;
#define MAX 1000
int map[MAX+1][MAX+1];
int visited[MAX+1];
int N,M,K;
void dfs(int start)
{
int i;
visited[start] = true;
for(i=1;i<=N;i++)
if ((!visited[i])&&(map[start][i]==1))
{
//printf("i=%d\n",i);
dfs(i);
}
}
void init()
{
int i,j,a,b;
scanf("%d %d %d",&N,&M,&K);
for(i=1;i<=N;i++)
for(j=1;j<=N;j++)
map[i][j] = 0;
for(i=0;i<M;i++)
{
scanf("%d %d",&a,&b);
map[a][b] = 1;
map[b][a] = 1;
}
}
int main()
{
int i,j,k,lost,total;
init();
for(i=0;i<K;i++)
{
scanf("%d",&lost);
total = 0;
for(j=1;j<=N;j++) visited[j] = false;
visited[lost] = true;
for(j=1;j<=N;j++)
if (!visited[j])
{
dfs(j);
total++;
}
printf("%d\n",total-1);
}
return 0;
}