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 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 Specification:

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 Specification:

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

 

题目大意: 有一些点,代表一些城市。同时给出了城市之间的道路。现在假设一些城市被叛军占据,那么问需要补建几条道路,使所有剩下的城市全部联通。

解题思路:这道题的考察的是联通块的知识。一个联通块内,所有城市都能够响度到达,所以一个连通块内的城市肯定不需要再修建道路了,只有不同连通块之间的才需要修建道路。因此,需要修建的道路 = 连痛块数目-1。因此只需要通过深度优先搜索求被叛军占据后,图的连痛块的数目就可以了。

 1 #include <iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 int e[1005][1005];
 5 int vis[1005];
 6 int n,m,k;
 7 void dfs(int node)
 8 {
 9     for(int i=1;i<=n;i++)
10     {
11         if(!vis[i]&&e[node][i])
12         {
13             vis[i] = 1;
14             dfs(i);
15         }
16     }
17     return;
18 }
19 int main()
20 {
21     int v1,v2;
22     scanf("%d %d %d",&n,&m,&k);
23     for(int i=0;i<m;i++)
24     {
25         scanf("%d %d",&v1,&v2);
26         e[v1][v2] = e[v2][v1] = 1;
27     }
28     for(int i=0;i<k;i++)
29     {
30         int cnt=0,op;
31         scanf("%d",&op);
32         fill(vis,vis+1005,0);
33         vis[op] = 1;
34         for(int i=1;i<=n;i++)
35         {
36             if(!vis[i])
37             {
38                 vis[i] = 1;
39                 dfs(i);
40                 cnt++;
41             }
42         }
43         printf("%d\n",cnt-1);
44     }
45     return 0;
46 }