团体程序设计天梯赛 L2-013 红色警报 (25分)
题目链接:
L2-013 红色警报 (25分)
思路:
每失去一个城市,dfs一遍,检查当前的连通块个数,如果比之前多,则需要警报;
复杂度
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 505;
bool lost[maxn], vst[maxn];
vector<int> G[maxn];
int n, m, ans;
void dfs(int u) {
vst[u] = true;
for(int & x : G[u]) if(!lost[x] && !vst[x]) dfs(x);
}
int test() {
int res = 0;
for(int i = 0; i < n; i++) vst[i] = false;
for(int i = 0; i < n; i++) {
if(lost[i]) continue;
if(!vst[i]) ++res, dfs(i);
}
return res;
}
int main() {
#ifdef MyTest
freopen("Sakura.txt", "r", stdin);
#endif
cin >> n >> m;
for(int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
int k;
cin >> k;
ans = test();
for(int i = 0; i < k; i++) {
int x;
cin >> x;
lost[x] = true;
int now = test();
if(now > ans) printf("Red Alert: City %d is lost!\n", x);
else printf("City %d is lost.\n", x);
ans = now;
}
if(k == n) puts("Game Over.");
return 0;
}