It is very important to maintain connectivity between cities during the war. This question requires you to write an alarm program. When the loss of a city causes the country to be divided into multiple unconnected areas, a red alarm will be issued. Note: If the country is not completely connected, it is divided into k regions, and losing a city does not change the connectivity between other cities, then do not issue an alarm.
Input format:
Enter two integers N
(0 < N
≤ 500) and M
(≤ 5000) in the first line , which are the number of cities (the default city is N
numbered from 0 to -1) and the number of roads connecting the two cities. On subsequent M
lines, each line gives the numbers of the two cities connected by a pathway, separated by a space. After the city information, the captured information is given, that is, a positive integer K
followed by the K
number of the captured city.
Note: The input guarantees that the given city numbers that are captured are legal and non-repetitive, but it does not guarantee that the given channels are not duplicated.
Output format:
For each captured city, if it will change the connectivity of the entire country, output it Red Alert: City k is lost!
, where it k
is the city number; otherwise, just output it City k is lost.
. If the country loses its last city, add a line of output Game Over.
.
Input sample:
5 4
0 1
1 3
3 0
0 4
5
1 2 0 4 3
Sample output:
City 1 is lost.
City 2 is lost.
Red Alert: City 0 is lost!
City 4 is lost.
City 3 is lost.
Game Over.
First use the union search to find the initial number of connected components C, and then re-calculate the number of connected components C1 without capturing a city (note that the edges of the captured city are skipped), each time C1 is compared with C, and the corresponding result is output. C1 C has 3 situations,
1. C1=C indicates that the captured city is in a ring, and it will not affect the connectivity of the original image.
2. C1=C+1 indicates that the captured city is only connected to one city, which is the outermost layer of the figure
3. C1-C >1, indicating that the captured city is connected to at least two cities. After being captured, the connected component will increase by at least 2.
Error-prone: 1. Note that the capture of a city is not to delete it, but not to count the edges it connects.
2. Each time it is compared with the previous connected component before the city is captured, and pay attention to updating the initial connected component.
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
pair<int,int>p[5500];
int pre[550];
int v[550];
int count(int n)
{
int c=0;
for(int i=0;i<n;i++)
{
if(v[i]==i)
c++;
}
return c;
}
int find(int root)
{
if(v[root]==root)
return root;
else
return v[root]=find(v[root]);
}
void Union(int r1,int r2)
{
int root1=find(r1);
int root2=find(r2);
if(root1!=root2)
v[root1]=root2;
}
int main()
{
int N,M;
cin>>N>>M;
for(int j=0;j<550;j++)
v[j]=j;
memset(pre,0,sizeof(pre));
for(int i=0;i<M;i++)
{
cin>>p[i].first>>p[i].second;
Union(p[i].first,p[i].second);
}
int r1=count(N);
int k;
cin>>k;
while(k--)
{
int pr;
cin>>pr;
pre[pr]=1;
for(int j=0;j<550;j++)
v[j]=j;
for(int j=0;j<M;j++)
{
if(pre[p[j].first]==1||pre[p[j].second]==1)
continue;
Union(p[j].first,p[j].second);
}
int r2=count(N);
if(r1+1==r2||r1==r2)
cout<<"City "<<pr<<" is lost."<<endl;
else
cout<<"Red Alert: City "<<pr<<" is lost!"<<endl;
r1=r2;
}
r1=count(N);
if(r1==N)
cout<<"Game Over."<<endl;
return 0;
}