首页 > 代码库 > PAT 甲级 1013 Battle Over Cities

PAT 甲级 1013 Battle Over Cities

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #define MAX 1001
 5 int a[MAX][MAX];
 6 int visited[MAX];
 7 int n, m, k;
 8 
 9 using namespace std;
10 
11 int dfs(int x)
12 {
13     visited[x]=1;
14     for (int i=1; i<=n; i++)
15     {
16         if (!visited[i]&&a[x][i]==1)
17         {
18             dfs(i);
19         }
20     }
21 }
22 int main()
23 {
24     scanf ("%d%d%d", &n, &m, &k);
25     for (int i=0; i<m; i++)
26     {
27         int x, y;
28         scanf ("%d%d", &x, &y);
29         a[x][y] = 1;
30         a[y][x] = 1;
31     }
32     for (int i=0; i<k; i++)
33     {
34         int mm;
35         scanf ("%d", &mm);
36         visited[mm]=1;
37 
38         int ans=0;
39 
40         for (int j=1; j<=n; j++)
41         {
42             if (!visited[j])
43             {
44                 dfs(j);
45                 ans++;
46             }
47         }
48         cout <<ans-1 <<endl;
49         memset(visited, 0, sizeof(visited));
50     }
51     return 0;
52 }

 

PAT 甲级 1013 Battle Over Cities