7-9 红色警报 (25分)
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<iomanip>
#include<stack>
using namespace std;
#define STDIN freopen("in.in", "r", stdin);freopen("out.out", "w", stdout);
const int N = 505;
int n, m;
int fa[N];
void init()
{
for (int i = 0; i < n; i++) fa[i] = i;
}
int find(int x)
{
if (x == fa[x]) return x;
return fa[x] = find(fa[x]);
}
void unite(int a, int b)
{
int f1 = find(a);
int f2 = find(b);
fa[f1] = f2;
}
vector<pair<int,int> > edge;
set<int> city;
vector<string> ans;
vector<int> city2;
map<int, vector<int> > ma;
string change(int j)
{
if (j == 0) return "0";
string t = "";
while (j)
{
t += j%10 + '0';
j/=10;
}
reverse(t.begin(), t.end());
return t;
}
int main()
{
// STDIN
cin >> n >> m;
init();
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
edge.push_back({a, b});
ma[a].push_back(b);
ma[b].push_back(a);
}
int k;
cin >> k;
for (int i = 1; i <= k; i++)
{
int t;
cin >> t;
city.insert(t);
city2.push_back(t);
}
if (k == n) ans.push_back("Game Over.");
for (int i = 0; i < m; i++)
{
if (city.find(edge[i].first) == city.end() && city.find(edge[i].second) == city.end())
{
unite(edge[i].first, edge[i].second);
}
}
reverse(city2.begin(), city2.end());
for (int i = 0; i < k; i++)
{
city.erase(city2[i]);
int cnt = 0;
for (auto &j : ma[city2[i]])
{
if (city.find(j) == city.end())
{
if (find(j) != find(city2[i]))
{
cnt++;
unite(j, city2[i]);
}
}
}
if (cnt > 1){
string s = "Red Alert: City ";
string send = " is lost!";
string smid = change(city2[i]);
s += smid + send;
ans.push_back(s);
}
else{
string s = "City ";
string send = " is lost.";
string smid = change(city2[i]);
s += smid + send;
ans.push_back(s);
}
}
reverse(ans.begin(), ans.end());
for (auto &i : ans) cout << i << endl;
}