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 (<), 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 Knumbers, 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,即所需的道路,求连通块用到了dfs
tips:每测试一种情况结束后记得恢复原状

代码如下:
 1 #include <iostream>
 2 #include <cstring>
 3 #include <vector>
 4 using namespace std;
 5 
 6 int n, m, k;
 7 bool city_map[1005][1005];
 8 bool vis[1005];
 9 vector<int> lost_city_connect;
10 
11 
12 void DeleteLostCity(int city)
13 {
14     for (int i = 1; i <= n; i++)
15     {
16         if (city_map[city][i] == true)
17         {
18             city_map[city][i] = false;
19             lost_city_connect.push_back(i);
20         }
21     }
22     for (int i = 1; i <= n; i++)
23     {
24         city_map[i][city] = false;
25     }
26 }
27 
28 void BackUpCity(int city)
29 {
30     for(auto it=lost_city_connect.begin();it!=lost_city_connect.end();it++)
31     {
32         city_map[city][*it]=city_map[*it][city]=true;
33     }
34     lost_city_connect.clear();
35 }
36 void DFS(int pos)        //求出该连通块 
37 {
38     vis[pos]=true;
39     for(int i=1;i<=n;i++)
40     {
41         if(city_map[pos][i]&&!vis[i])
42         {
43             DFS(i);
44         }
45     }
46 }
47 int main()
48 {
49     cin >> n >> m >> k;
50     for (int i = 0; i < m; i++)
51     {
52         int a, b;
53         scanf("%d %d", &a, &b);
54         city_map[a][b] = city_map[b][a] = true;
55     }
56     bool output_flag=false;
57     for (int i = 0; i < k; i++)
58     {
59         int lost_city;
60         scanf("%d", &lost_city);
61         DeleteLostCity(lost_city);
62         if(output_flag==false)    output_flag=true;
63         else printf("
");
64         int count=0;
65         for(int i=1;i<=n;i++)
66         {
67             if(!vis[i]&&i!=lost_city)
68             {
69                 DFS(i);
70                 count++;
71             }
72         }
73         printf("%d",count-1);
74         memset(vis,false,sizeof(vis));
75         BackUpCity(lost_city);
76     }
77     return 0;
78 }

 


 
原文地址:https://www.cnblogs.com/jiongyy0570/p/10448114.html