PAT 2021.4.20.2. 红色警报
原题链接
简单
作者:
DPH
,
2021-04-21 21:26:37
,
所有人可见
,
阅读 352
#include <iostream>
#include <queue>
#include <vector>
#include <math.h>
#include <string.h>
#include <algorithm>
#define pb push_back
using namespace std;
const int N = 500 + 10;
int n, m, f[N];
bool g[N][N];
int Find(int x)
{
if(x == f[x]) return x;
return f[x] = Find(f[x]);
}
void Union(int x, int y)
{
int xx = Find(x); int yy = Find(y);
if(xx != yy) f[xx] = yy;
return ;
}
int main()
{
cin>>n>>m;
for(int i = 0; i < n; i++) f[i] = i;
while(m--)
{
int a, b; cin>>a>>b;
g[a][b] = 1; g[b][a] = 1;
Union(a, b);
}
int cnt = 0;
for(int i = 0; i < n; i++)
{
if(f[i] == i) cnt++;
}
int k; cin>>k; int cnt1 = 0;
while(k--)
{
for(int i = 0; i < n; i++) f[i] = i;
int t; cin>>t;
for(int i = 0; i < n; i++) g[i][t] = 0, g[t][i] = 0;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(g[i][j]) Union(i, j);
}
}
for(int i = 0; i < n; i++)
{
if(f[i] == i) cnt1++;
}
if(cnt1 > cnt + 1) cout<<"Red Alert: City "<<t<<" is lost!\n";
else cout<<"City "<<t<<" is lost.\n";
cnt = cnt1; cnt1 = 0;
}
if(cnt == n) cout<<"Game Over.\n";
return 0;
}