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 TO, 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 TO numbers, which represent the cities we concern.
Output Specification:

For each of the TO 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
No blank line at the end

Sample Output:

1
0
0
No blank line at the end
题目大意:总共有N个城市,M条道路,T0个检测点,问每个检测点(城市)被攻占之后,最少需要修多少条高速公路来恢复之前的连接。例如样例:
共3个城市,2条道路,3个检测点(城市)
城市1-城市2相连
城市1-城市3相连
如果城市1被攻占,则城市1和城市2、城市3之间的道路就会被切断,因此如果想要原来的城市保持连接,必须把1和3连起来,最少需要1条道路
如果城市2被攻占,因为1和3本来有道路连接,所以不用修建,所以最少需要0条道路
同理城市3最少需要0条道路
因此答案为
1
0
0
思路:联通块问题,采用并查集算法来解决,先把之前的道路用一个pair容器来存储,每次询问一个检测点(城市)进行判断,遍历容器pair,如果这两个城市都没被攻占,直接相连,合并联通块。最后统计联通块的个数,应该减去2(因为被攻占的城市算一个联通块被多算了,而且n个联通块之间只需要n-1条道路连接,因此最后答案为n-2)

#include <bits/stdc++.h>
using namespace std;
int pre[1001],N,M,T;
pair<int,int> P[1000001];//因为只有两个变量,采用paur容器存储两个相邻点
void Init() //并查集初始化
{
	for(int i=1;i<=N;i++)
	 pre[i]=i;
}
int find_boss(int x) //找老大
{   
	if(pre[x]==x)
	  return x;
	else 
	  return pre[x]=find_boss(pre[x]);
}
void merge(int x,int y)//合并
{   
	int a=find_boss(x),b=find_boss(y);
	pre[b]=a;
}
int judge(int x)//计算每个检测点
{   int count1=0;
	for(int i=1;i<=M;i++)
	{  if(P[i].first!=x&&P[i].second!=x)//如果这两个城市都没被攻占,直接相连
	     merge(P[i].first,P[i].second);
	}
	for(int i=1;i<=N;i++)
	  if(pre[i]==i)
	    count1++;//  最后统计联通块的个数
	if(count1>1)
	return count1-2;
	else return 0;
}
int main()
{
	cin>>N>>M>>T;
	for(int i=1;i<=M;i++)
	  {  int t1,t2;
	     cin>>t1>>t2;
		 P[i].first=t1;
		 P[i].second=t2;
	  }
	for(int i=1;i<=T;i++)//对每一个检测点都用一次并查集合并
	{   int t,ans;
		Init();
		cin>>t;
		ans=judge(t);
		if(i==T)
		cout<<ans;
		else
		cout<<ans<<endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乔梦圆的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值