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 }
时间: 2024-10-03 22:54:34

PAT 甲级 1013 Battle Over Cities的相关文章

PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)

1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any oth

PAT Advanced 1013 Battle Over Cities (25分)

It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of

PAT:1013. Battle Over Cities (25) AC

#include<stdio.h> #include<string.h> #include<vector> using namespace std; const int MAX=1010; int n,m,k; //城市数,高速公路数,查询次数 int DELETE; //要删除的点 vector<int> ADJ[MAX]; //邻接表 bool vis[MAX]; void DFS(int s) { if(DELETE==s) return; //表示该

1013. Battle Over Cities (25)——PAT (Advanced Level) Practise

题目信息: 1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward th

PAT Advanced Level 1013 Battle Over Cities (25)(25 分)

1013 Battle Over Cities (25)(25 分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any

pat 1013 Battle Over Cities(25 分) (并查集)

1013 Battle Over Cities(25 分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any othe

pat甲级1013

1013 Battle Over Cities (25)(25 分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any

1013 Battle Over Cities (25分) DFS | 并查集

1013 Battle Over Cities (25分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any othe

1013——Battle Over Cities

It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of