PAT1013. Battle Over Cities

1013. Battle Over Cities (25)

时间限制

400 ms

内存限制

65536 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 that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly.

For example, if we have 3 cities and 2 highways connecting city1-city2 and city1-city3. Then if city1 is occupied by the enemy, we must have 1 highway repaired, that is the highway city2-city3.

Input

Each input file contains one test case. Each case starts with a line containing 3 numbers N (<1000), M and K, which are the total number of cities, the number of remaining highways, and the number of cities to be checked, respectively. Then M lines follow, each describes a highway by 2 integers, which are the numbers of the cities the highway connects. The cities are numbered from 1 to N. Finally there is a line containing K numbers, which represent the cities we concern.

Output

For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.

Sample Input

3 2 3
1 2
1 3
1 2 3

Sample Output

1
0
0思路:要时刻记住一个DFS可以访问一个连通块。最后的连接是一个连通块的个数减一

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 #define MAX 1010
 6 bool G[MAX][MAX];
 7 bool visited[MAX];
 8 int N,M,K;
 9 int currentpoint;
10 void Init()
11 {
12     for(int i=1;i<=N;i++)
13       visited[i]=false;
14 }
15 void DFS(int index)
16 {
17     if(index==currentpoint)
18         return ;
19     visited[index]=true;
20     for(int j=1;j<=N;j++)
21     {
22         if(!visited[j]&&G[index][j])
23            DFS(j);
24     }
25 }
26 int main(int argc, char *argv[])
27 {
28     scanf("%d%d%d",&N,&M,&K);
29     for(int i=0;i<M;i++)
30     {
31         int a,b;
32         scanf("%d%d",&a,&b);
33         G[a][b]=G[b][a]=true;
34     }
35     for(int i=0;i<K;i++)
36     {
37         scanf("%d",&currentpoint);
38         int block=0;
39         memset(visited,false,sizeof(visited));
40         visited[currentpoint]=true;
41         for(int i=1;i<=N;i++)
42         {
43             if(!visited[i])
44             {
45                 DFS(i);
46                 block++;
47             }
48         }
49         printf("%d\n",block-1);
50     }
51     return 0;
52 }

时间: 2024-08-05 20:34:56

PAT1013. Battle Over Cities的相关文章

PAT1013. 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 Battle Over Cities (25 分) DFS求连通块

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

PAT1013. Battle Over Cities(邻接矩阵、邻接表分别dfs)

//采用不同的图存储结构结构邻接矩阵.邻接表分别dfs,我想我是寂寞了吧,应该试试并查集,看见可以用并查集的就用dfs,bfs代替......怕了并查集了 //邻接矩阵dfs #include<cstdio>#include<algorithm>using namespace std;const int maxn=1001;int g[maxn][maxn];int n,tmp;bool vis[maxn];void dfs(int v){ vis[v]=true; for(int

PAT-1013 Battle Over Cities (25)

求解连通性问题,最好用的当然是并查集了,可以使用深搜或者广搜. 这道题目的意思是给定一些道路,如果把其中一个顶点去掉,那么需要建立多少条道路才能联通所有顶点. 这道题目如果用朴素的并查集的话第四个测试用例会超时,因此想到带路径压缩的并查集.递归或者非递归方式都可以,对于这道题目来说不会差别很大,不过用递归可能会有栈溢出的问题,当数据量小的时候没有什么大问题.(其实递归的深度不会很大,所以RE得风险应该很小的,已建立起来的数目只有两层.错误,比如两个帮派老大带了一群小弟,一个帮派老大投靠了另外一个

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

PAT-Top1001. Battle Over Cities - Hard Version (35)

在敌人占领之前由城市和公路构成的图是连通图.在敌人占领某个城市之后所有通往这个城市的公路就会被破坏,接下来可能需要修复一些其他被毁坏的公路使得剩下的城市能够互通.修复的代价越大,意味着这个城市越重要.如果剩下的城市无法互通,则说明代价无限大,这个城市至关重要.最后输出的是代价最大的城市序号有序列表.借助并查集和Kruskal算法(最小生成树算法)来解决这个问题. 1 //#include "stdafx.h" 2 #include <iostream> 3 #include

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