码迷,mamicode.com
首页 > 其他好文 > 详细

PAT1013. Battle Over Cities

时间:2015-02-27 11:41:27      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:

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 }
View Code

 

PAT1013. Battle Over Cities

标签:

原文地址:http://www.cnblogs.com/GoFly/p/4302874.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!