# L2-013. 红色警报（并查集~）

L2-013. 红色警报

400 ms

65536 kB

8000 B

Standard

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.

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
int n,m;
struct node
{
int u,v;
} e[5005];
int vist[550],fa[550];
int Find(int x)
{
return x==fa[x]?fa[x]:Find(fa[x]);
}
void Union(int x,int y)
{
int xc=Find(x);
int yc=Find(y);
if(xc!=yc)
{
fa[xc]=yc;
}
}
int main()
{
int k;
int u,v;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=0; i<n; i++)
fa[i]=i;
for(int i=0; i<m; i++)
{
scanf("%d%d",&u,&v);
e[i].u=u,e[i].v=v;
Union(u,v);
}
int num=0,num1;
for(int i=0; i<n; i++)
{
if(fa[i]==i)
{
num++;
}
}
memset(vist,0,sizeof(vist));
scanf("%d",&k);
while(k--)
{
num1=0;
for(int i=0;i<n;i++)
fa[i]=i;
int x;
scanf("%d",&x);
vist[x]=1;
for(int i=0;i<m;i++)
{
if(vist[e[i].u]==1||vist[e[i].v]==1)
continue;
else
Union(e[i].u,e[i].v);
}
for(int i=0;i<n;i++)
if(fa[i]==i)
num1++;
if(num==num1||num+1==num1)
printf("City %d is lost.\n",x);
else
printf("Red Alert: City %d is lost!\n",x);
num=num1;
}
num=0;
for(int i=0;i<n;i++)
if(vist[i]==1)
num++;
if(num==n)
printf("Game Over.\n");
}
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120