题目链接:https://www.patest.cn/contests/gplt/L2-013
战争中保持各个城市间的连通性非常重要。本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报。注意:若该国本来就不完全连通,是分裂的k个区域,而失去一个城市并不改变其他城市之间的连通性,则不要发出警报。
输入格式:
输入在第一行给出两个整数N(0 < N <=500)和M(<=5000),分别为城市个数(于是默认城市从0到N-1编号)和连接两城市的通路条数。随后M行,每行给出一条通路所连接的两个城市的编号,其间以1个空格分隔。在城市信息之后给出被攻占的信息,即一个正整数K和随后的K个被攻占的城市的编号。
注意:输入保证给出的被攻占的城市编号都是合法的且无重复,但并不保证给出的通路没有重复。
输出格式:
对每个被攻占的城市,如果它会改变整个国家的连通性,则输出“Red Alert: City k is lost!”,其中k是该城市的编号;否则只输出“City k is lost.”即可。如果该国失去了最后一个城市,则增加一行输出“Game Over.”。
输入样例:5 4 0 1 1 3 3 0 0 4 5 1 2 0 4 3输出样例:
City 1 is lost. City 2 is lost. Red Alert: City 0 is lost! City 4 is lost. City 3 is lost. Game Over.
时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越
乍看一下不简单,但是还是并查集的应用, #include<stdio.h>
#include<string.h>
#include<math.h>
//#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define da 0x3f3f3f3f
#define xiao -0x3f3f3f3f
#define clean(a,b) memset(a,b,sizeof(a))// 雷打不动的头文件
struct ac{
int a,b;
}shuzu[5500];
bool city[550];
int per[550];
int find(int x)
{
while(per[x]!=x)
x=per[x];
return x;
}
void mix(int a,int b)
{
int aa=find(a);
int bb=find(b);
per[aa]=per[bb];
}
void intt()
{
int i,j;
for(i=0;i<550;++i)
per[i]=i;
}
int main()
{
int n,m;
cin>>n>>m;
int i,j;
intt();
/*for(i=0;i<550;++i)
per[i]=i;*/
for(i=0;i<m;++i)
{
//int a,b;
cin>>shuzu[i].a>>shuzu[i].b;
mix(shuzu[i].a,shuzu[i].b); //路线链接;
}
int lu=0;
for(i=0;i<n;++i)
{
if(per[i]==i)
lu++; //目前有lu条路线;
}
int k;
cin>>k;
clean(city,0);
for(j=0;j<k;++j) //k次攻城
{
intt();
/*for(i=0;i<550;++i)
per[i]=i;*/
int can;
cin>>can;
city[can]=1; //该城市已被攻陷
for(i=0;i<m;++i)
{
/*if(shuzu[i].a==can)
shuzu[i].a=shuzu[i].b;
else if(shuzu[i].b==can)
shuzu[i].b=shuzu[i].a;
mix(shuzu[i].a,shuzu[i].b);*/
if(city[shuzu[i].a]==0&&city[shuzu[i].b]==0)//该路线链接的两个城市没被攻陷
mix(shuzu[i].a,shuzu[i].b); //连接在一起的路线是一条路
}
int canlu=0; //独立的路线初始0;
for(i=0;i<n;++i)
{
if(city[i]==0&&per[i]==i) //独立的路线&&该城市没被攻陷
canlu++;
/*if(i==can)
continue;
if(per[i]==i)
canlu++;*/
}
if(canlu==lu||canlu==lu-1) //路线不变||城市攻陷一个(路线减少一条)
printf("City %d is lost.\n",can);//cout<<"City "<<can<<" is lost."<<endl;
else
printf("Red Alert: City %d is lost!\n",can);//cout<<"Red Alert: City "<<can<<" is lost!"<<endl;
lu=canlu; //重置一下目前的路线
if(j==n-1) //城市最后一个也被攻陷了
printf("Game Over.\n");//cout<<"Game Over."<<endl;
}
}