博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
L2-013 红色警报 (25 分)
阅读量:388 次
发布时间:2019-03-05

本文共 4277 字,大约阅读时间需要 14 分钟。

L2-013 红色警报 (25 分)

题目

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

先用并查集算出初始有多少连通分量并用pair存储所有的路径,之后每次将被攻占的城市标记,并在初始化并查集后重新遍历所有路径,如果路径端点已经有一个以上被攻占,那么这条路径不再被连通,由此构建新的并查集并再次计算连通分量,若当前连通分量小于等于(上次连通分量的值+1),说明没有改变连通性(等于上次的连通分量说明被攻占节点本来就已经孤立,等于上次加一说明被攻占节点本来属于一个连通分量,现在单独一个连通分量,所以加一);否则改变。Game Over的判断就是判断已被攻占的城市数量是不是等于所有城市数量。

2、dfs

参考:

因为个人的dfs和bfs确实很差劲,所以又用dfs做了一遍。和并查集其实是一样的,都只是算连通分量的工具而已。

图的存储就不用说了,正常做就行,要多存的是每一对路径(Pair), 以及当前城市是否被攻占。第一次dfs时就是正常dfs,有城市失守后,在dfs到已失守节点时直接跳过(即当成孤立)。连通分量变化的判断和并查集是一样的。

代码

1、并查集
#include
#define INF 0x3f3f3f3f#define PI acos(-1)#define pb push_backusing namespace std;typedef pair
P;typedef long long ll;const int N = 2e5 + 19;const ll mod = 1e9 + 7;int par[N];int rnk[N];void init(int n){
for(int i = 0; i < n + 1; i++) {
par[i] = i; }}int find(int x){
if(par[x] == x) return x; return par[x] = find(par[x]);}void unite(int x, int y){
x = find(x); y = find(y); if(x == y) return ; if(rnk[x] < rnk[y]) {
par[x] = y; } else {
par[y] = x; if(rnk[y] == rnk[x]) rnk[x]++; }}int lost[N];P road[N];int main(){
int n, m; cin >> n >> m; init(n); for(int i = 0, x, y; i < m; i++) {
cin >> x >> y; unite(x, y); road[i].first = x; road[i].second = y; } int cnt0 = 0; for(int i = 0; i < n; i++) {
if(par[i] == i) cnt0++; } int lostNum = 0; int k; cin >> k; for(int i = 0, x; i < k; i++) {
init(n); cin >> x; lost[x] = 1; lostNum++; for(int i = 0; i < m; i++) {
if(lost[road[i].first] || lost[road[i].second]) continue; unite(road[i].first, road[i].second); } int cnt = 0; for(int i = 0; i < n; i++) {
if(par[i] == i) cnt++; } if(cnt > cnt0 + 1) {
cout << "Red Alert: City " << x << " is lost!" << endl; } else {
cout << "City " << x << " is lost." << endl; } if(lostNum == n) {
cout << "Game Over." << endl; } cnt0 = cnt; } return 0;}
2、dfs
#include
#define INF 0x3f3f3f3f#define PI acos(-1)#define pb push_backusing namespace std;typedef pair
P;typedef long long ll;const int N = 2e5 + 19;const ll mod = 1e9 + 7;bool vis[505];vector
es[505];bool lost[505];P road[5010];void dfs(int x){
if(lost[x]) return ; for(int i = 0; i < es[x].size(); i++) {
if(lost[es[x][i]]) continue; if(!vis[es[x][i]]) {
vis[es[x][i]] = 1; dfs(es[x][i]); } }}int count(int n){
int cnt = 0; for(int i = 0; i < n; i++) {
if(!vis[i]) {
cnt++; dfs(i); } } return cnt;}int main(){
int n, m; cin >> n >> m; for(int i = 0, x, y; i < m; i++) {
cin >> x >> y; es[x].pb(y); es[y].pb(x); road[i].first = x; road[i].second = y; } int cnt0 = count(n); int lostNum = 0; int k; cin >> k; for(int i = 0, x; i < k; i++) {
fill(vis, vis + n + 1, 0); cin >> x; lost[x] = 1; lostNum++; int cnt = count(n); if(cnt > cnt0 + 1) {
cout << "Red Alert: City " << x << " is lost!" << endl; } else {
cout << "City " << x << " is lost." << endl; } if(lostNum == n) {
cout << "Game Over." << endl; } cnt0 = cnt; } return 0;}

转载地址:http://liwzz.baihongyu.com/

你可能感兴趣的文章