题目链接:点击打开链接
思路:每失去一个城市,判断连通分量是否增加,若增加,则发出红色警报。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int n,m,map[505][505];
int book[505],flag[505];
void bfs(int x){
queue<int> q;
q.push(x);
book[x] = 1;
while(!q.empty()){
int t = q.front();
q.pop();
for(int i = 0;i < n;i++){
if(flag[i] == 0 && map[t][i] == 1 && !book[i]){
q.push(i);
book[i] = 1;
}
}
}
}
int main(){
int x,y,cnt;
scanf("%d%d",&n,&m);
memset(map,0,sizeof(map));
memset(flag,0,sizeof(flag));
for(int i = 0;i < m;i++){
scanf("%d%d",&x,&y);
map[x][y] = map[y][x] = 1;
}
cnt = 0;
memset(book,0,sizeof(book));
for(int i = 0;i < n;i++){
if(!book[i]){
bfs(i);
cnt++;
}
}
int q,cnt1;
scanf("%d",&q);
for(int i = 0;i < q;i++){
scanf("%d",&x);
flag[x] = -1;
cnt1 = 0;
memset(book,0,sizeof(book));
for(int i = 0;i < n;i++){
if(flag[i] == 0 && !book[i]){
bfs(i);
cnt1++;
}
}
if(cnt1 > cnt){
printf("Red Alert: City %d is lost!\n",x);
}
else{
printf("City %d is lost.\n",x);
}
cnt = cnt1;
}
if(q == n){
printf("Game Over.\n");
}
return 0;
}