战争中保持各个城市间的连通性非常重要。本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报。注意:若该国本来就不完全连通,是分裂的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.

提交代码

自己写的时候在连接判断时判断错误。。。

判断所有点的连通块的个数,当删去两个点的关系后判断增加的连通块是否大于1,大于1证明有影响

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<bits/stdc++.h>
#define maxn 1010
#define debug(a) cout << #a << ": " << a << endl
using namespace std;
int vis[maxn],n,m,mapn[maxn][maxn];
void dfs( int node ) {
vis[node] = ;
for( int i = ; i < n; i ++ ) {
if( !vis[i] && mapn[node][i] ) {
dfs( i );
}
}
}
int countcnt() {
int cnt = ;
memset( vis, , sizeof( vis ) );
for( int i = ; i < n; i ++ ) {
if( !vis[i] ) {
dfs( i );
cnt ++;
}
}
return cnt;
}
int main() {
while( cin >> n >> m ) {
for( int i = ; i < n; i ++ ) {
for( int j = ; j < n; j ++ ) {
mapn[i][j] = ;
}
}
for( int i = ; i < m; i ++ ) {
int x, y;
cin >> x >> y;
mapn[x][y] = mapn[y][x] = ;
}
int T, cnt = countcnt();
cin >> T;
for( int i = ; i <= T; i ++ ) {
int t;
cin >> t;
for( int j = ; j < n; j ++ ) {
if( mapn[t][j] ) {
mapn[t][j] = mapn[j][t] = ;
}
}
int tmpcnt = countcnt();
if( tmpcnt > cnt + ) {
cout << "Red Alert: City " << t << " is lost!" << endl;
} else {
cout << "City " << t << " is lost." << endl;
}
cnt = tmpcnt;
if( i == n ) {
cout << "Game Over." << endl;
}
}
}
return ;
}

最新文章

  1. Android手机同步电脑端google chrome书签
  2. UVA 1626 Brackets sequence(括号匹配 + 区间DP)
  3. 2016年11月29日 星期二 --出埃及记 Exodus 20:20
  4. android Log 等级以及在Android Studio 的Logcat中过滤方法
  5. 打开FTP服务器上的文件夹时发生错误,请检查是否有权限访问该文件夹
  6. 【noip2012提高组】国王游戏
  7. python cmd 模块
  8. Vijos 1083 小白逛公园(线段树)
  9. Cassandra C++/NodeJs开发环境
  10. 部署github开源软件遇到的问题
  11. mysql 分库分表转
  12. MySQL学习——标识符语法和命名规则
  13. mint修改host
  14. [leetcode]432. All O`one Data Structure全O(1)数据结构
  15. springboot之单元测试
  16. inline、block与inline-block
  17. string字符串js操作
  18. [No000010B]Git4/9-时光机穿梭
  19. php 当前时间 当前时间戳和数据库里取出的时间datetime格式进行比较大小
  20. R因子

热门文章

  1. python 实现两个文本文件内容去重
  2. 有趣的Flex布局
  3. JDK容器类List,Set,Queue源码解读
  4. k8s+istio:流量控制之灰度发布
  5. Unity工程无代码化
  6. css实现左边高度自适应右边高度
  7. 转载 | Sublime text3 实用快捷键整理
  8. QT动画时间轴控制 QTimeLine
  9. Logback配置文件这么写,TPS提高10倍
  10. 洛谷 P3628 特别行动队