L2-013 Red Alert (25 points)
War to maintain connectivity between various cities is very important. This question requires you to write a program alarm, when the city lost a lead country is split into multiple zones can not communicate, it emits red alert. Note: If the country should not have full connectivity, k is divided regions, a lost city does not alter the connectivity between other cities, do not alarm.
Input formats:
Input two integers are given in the first row N
(0 < N
≤ 500) and M
(≤ 5000), respectively, the number of city (city then default from 0 to N
-1 numbered) and connections of the two cities via strip. Subsequently M
rows each given two cities connected via a number, separated by a space therebetween. It gives information was captured after city information, that is a positive integer K
and subsequently K
a number of cities were captured.
Note: Enter the city was captured guarantee given number is legitimate and no repeat, but does not guarantee passage given no repeat.
Output formats:
Each city was captured, if it will change the connectivity of the entire country, the output Red Alert: City k is lost!
, which k
is the city's number; otherwise, only the output City k is lost.
can be. If the country lost its last city, then add a line output Game Over.
.
Sample input:
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.
#include<iostream>
using namespace std;
int cnt ,n,mp[509][509] , vis[509] , v[509];
void dfs( int node ){
for( int i=0;i<n;i++)
if( !v[i] && mp[node][i] ){
v[i] = 1;
dfs( i );
}
}
int check( ){
for( int i=0 ; i<n ;i++)
v[i] = vis[i];
cnt =0;
for( int j = 0 ;j<n;j++){
if( !v[j] ){
cnt++;
dfs( j );
}
}
return cnt;
}
int main(void){
int m,a,b;
cin>>n>>m;
for( int i=1;i<=m;i++){
cin>>a>>b;
mp[a][b] = mp[b][a] =1;
}
int k;
cin>>k;
for( int i=1;i<=k;i++){
int cur = check( );
int node;
cin >>node;
vis[node] = 1;
int tmp = check( );
if( cur >= tmp )
cout<<"City "<<node<<" is lost."<<endl;
else {
cout<<"Red Alert: City "<<node<<" is lost!"<<endl;
}
if( i == n )
cout<<"Game Over."<<endl;
}
return 0;
}