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;
		}
	}
}

谢谢指出,上述代码最后一个测试点会超时,应该是最近pat把测试点改了,我去年十月份的时候是能通过的。。

然后,更新后的代码就是加了一行这个:std::ios::sync_with_stdio(false);

习惯使用c++语言风格的最好记一下,对于cin和cout的速度提高挺多,比较来看应该和c语言的输入输出差不多的样子,更新后的代码如下:

#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()
{
	std::ios::sync_with_stdio(false);  //取消cin与stdin的同步
	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;
		}
	}
	system("pause");
}

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值