战争中保持各个城市间的连通性非常重要。本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报。注意:若该国本来就不完全连通,是分裂的k个区域,而失去一个城市并不改变其他城市之间的连通性,则不要发出警报。

输入格式:

输入在第一行给出两个整数N(0 < N <=500)和M(<=5000),分别为城市个数(于是默认城市从0到N-1编号)和连接两城市的通路条数。随后M行,每行给出一条通路所连接的两个城市的编号,其间以1个空格分隔。在城市信息之后给出被攻占的信息,即一个正整数K和随后的K个被攻占的城市的编号。

注意:输入保证给出的被攻占的城市编号都是合法的且无重复,但并不保证给出的通路没有重复。

输出格式:

对每个被攻占的城市,如果它会改变整个国家的连通性,则输出“Red Alert: City k is lost!”,其中k是该城市的编号;否则只输出“City k is lost.”即可。如果该国失去了最后一个城市,则增加一行输出“Game Over.”。

输入样例:

5 4
0 1
1 3
3 0
0 4
5
1 2 0 4 3

输出样例:

City 1 is lost.
City 2 is lost.
Red Alert: City 0 is lost!
City 4 is lost.
City 3 is lost.
Game Over.

析:直接用并查集暴力即可,数据超水。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 500 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
bool vis[maxn];
vector<int> G[maxn];
int p[maxn];
inline int Find(int x){ return x == p[x] ? x : p[x] = Find(p[x]); }
bool cnt[maxn]; int main(){
scanf("%d %d", &n, &m);
for(int i = 0; i < n; ++i) p[i] = i;
for(int i = 0; i < m; ++i){
int u, v;
scanf("%d %d", &u, &v);
G[u].push_back(v);
int x = Find(u);
int y = Find(v);
if(x != y) p[y] = x;
}
scanf("%d", &m);
for(int j = 0; j < m; ++j){
int t;
scanf("%d", &t);
vis[t] = 1;
int tt = Find(t);
memset(cnt, 0, sizeof cnt);
for(int i = 0; i < n; ++i) if(!vis[i] && Find(i) == tt) cnt[i] = 1;
for(int i = 0; i < n; ++i) if(cnt[i]) p[i] = i;
for(int i = 0; i < n; ++i) if(cnt[i]){
int x = Find(i);
for(int k = 0; k < G[i].size(); ++k) if(cnt[G[i][k]]){
int y = Find(G[i][k]);
if(x != y) p[y] = x;
}
}
set<int> sets;
for(int i = 0; i < n; ++i) if(cnt[i]) sets.insert(Find(i));
if(sets.size() < 2) printf("City %d is lost.\n", t);
else printf("Red Alert: City %d is lost!\n", t);
}
if(m == n) puts("Game Over.");
return 0;
}

最新文章

  1. shell--3.运算符
  2. [转] MemCached 的 stats 命令
  3. 使用html5的离线缓存技术
  4. 闲谈Tomcat性能优化
  5. inline和宏之间的区别
  6. 14_Response对象
  7. java中关于时间的格式化
  8. css 不确定元素宽度的水平居中
  9. Maven手动创建多模块项目
  10. C/C++语言的标准库函数malloc/free与运算符new/delete的区别
  11. http协议详解(超详细)
  12. tensorflow softmax_cross_entropy_with_logits函数
  13. PLECS_晶闸管调速系统_9w
  14. 【转】CGI
  15. 谷歌浏览器中安装Axure扩展程序
  16. windows chocolatey 修改默认安装软件默认安装路径
  17. sublime3安装ctags追踪插件
  18. ELK技术实战-安装Elk 5.x平台
  19. 【BZOJ】1187: [HNOI2007]神奇游乐园
  20. UIScrollview的 约束 contentsize contentoffset contentinset layoutsubviews needlayout等影响布局的属性

热门文章

  1. java中随机生成汉字
  2. Mac版 Intellij IDEA 激活
  3. 不依赖外部js es 库 实现 点击内容 切换
  4. shell实现mysql热备份
  5. coreseek中文搜索
  6. discuz论坛搬家
  7. 采集练习(十一) php 获得电视节目预告---数据来自电视猫
  8. File.basename
  9. 【css学习整理】浮动,清除
  10. nginx.config配置文件模板