pat甲级1013 参考柳神思路

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 city​1​​-city​2​​ and city​1​​-city​3​​. Then if city​1​​ is occupied by the enemy, we must have 1 highway repaired, that is the highway city​2​​-city​3​​.

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

       题目的大概意思就是说现在给我们一些个城市,城市数量用N来表示,然后再给我们一些连接这些城市的路,路的数量用M来表示,然后这些个城市正在打仗,某一个城市可能被敌方所占领,当该城市被敌方占领的时候它连接其他城市的道路也就会被摧毁,导致无法使用,而我们要做的就是要得到当某个城市被敌方所占领后,为了使我们的城市之间互相连通,我们一共需要修建多少条公路?

       大概思路就是求这个图中把某一个城市除去后的连通分量(connected component),设该连通分量为cc,则该题中所求的需要修建的道路的数量为cc-1,因为如果有5座城市,一共只需4条路就可完成对这五座城市的连接。对图的存储我采用的是邻接矩阵,此处可能存在对内存的浪费,但最后还是成功通过,而遍历的话我采用的是深度优先遍历(DFS),采用广度优先遍历( BFS )应该也是可以的,在此就不赘述了。

#include<iostream>
#include<vector>
using namespace std;
void DFS(vector<bool> &visit, vector<vector<bool>> &E, int index)  //深度优先遍历递归算法,index为访问的城市
{
	visit[index] = true;
	for (int i = 1; i < visit.size(); i++)
	{
		if (E[index][i] ==true && visit[i]==false)
		{
			DFS(visit, E, i);
		}
	}
}
int main()
{
	int N, M, K;
	cin >> N >> M >> K;
	vector<vector<bool>> E(N+1);   //边 
	vector<bool> visit(N + 1);     //访问标记
	vector<int> outPut(K);         //待求解城市数组
	for (int i = 0; i <= N; i++)
	{
		E[i].resize(N+1);
	}
	for (int i =0; i < M; i++)
	{
		int a, b;
		cin >> a >> b;
		E[a][b] = E[b][a] =true;
	}
	for (int i = 0; i < K; i++)    //对想要计算的城市进行记录
	{
		int a;
		cin >> a;
		outPut[i] = a;
	}
	for (int i = 0; i < K; i++)
	{
		for (int v = 1; v < visit.size(); v++)
		{
			visit[v] = false;
		}
		int cnt=0;         //图的连通分量
		visit[outPut[i]] = true;
		for (int j = 1; j <= N; j++)
		{
			if (!visit[j])            
			{
				DFS(visit, E, j);         //从一个点开始对一个连通分量进行遍历
				cnt++;                    //连通分量数加一
			}
		}
		if (i < K - 1)
		{
			cout << cnt - 1 << endl;
		}
		else
		{
			cout << cnt-1;
		}
	}
}

 

    原文作者:算法
    原文地址: https://www.twblogs.net/a/5bd3b4ac2b717778ac20b939
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞