战争中保持各个城市间的连通性非常重要。本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报。注意:若该国本来就不完全连通,是分裂的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.
用邻接矩阵存图,dfs求连通块的数量,或者每次用并查集?
题意不清楚,狗住我了
/*
A: 10min
B: 20min
C: 30min
D: 40min
*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <sstream>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define mem(f, x) memset(f,x,sizeof(f))
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo_(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
template<typename T>
ostream& operator<<(ostream& os,const vector<T>&v){for(int i=0,j=0;i<v.size();i++,j++)if(j>=5){j=0;puts("");}else os<<v[i]<<" ";return os;}
template<typename T>
ostream& operator<<(ostream& os,const set<T>&v){for(auto c:v)os<<c<<" ";return os;}
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const map<T1,T2>&v){for(auto c:v)os<<c.first<<" "<<c.second<<endl;return os;}
template<typename T>inline void rd(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
typedef pair<int,int>PII;
typedef pair<long,long>PLL;
typedef long long ll;
typedef unsigned long long ull;
const int N=510,M=10010,inf=0;
ll n,m,_;
int g[N][N];
bool st[N];
void dfs(int u){
st[u]=1;
for(int i=1;i<=n;i++){
if(!st[i]&&g[u][i]==1){
dfs(i);
}
}
}
int cal(){
int cnt=0;
for(int j=1;j<=n;j++)st[j]=0;
for(int i=1;i<=n;i++){
if(!st[i]){
// cout<<"!"<<i<<endl;
dfs(i);
cnt++;
}
}
return cnt;
}
void solve(){
cin>>n>>m;
while(m--){
int a,b;cin>>a>>b;
a++,b++;
g[a][b]=g[b][a]=1;
}
int k;cin>>k;
int count=cal();
bool flag = (k==n?1:0);
while(k--){
int x;cin>>x;
x++;
for(int i=1;i<=n;i++){
g[i][x]=inf;
g[x][i]=inf;
}
// int y = cal();// 这个点被攻陷,一定会被孤立的
// if(y-1 > count){
// printf("Red Alert: City %d is lost!\n",x-1);
// }
// else{
// printf("City %d is lost.\n",x-1);
// }
int y = cal()-1;// 这个点被攻陷,一定会被孤立的
if(y !=count){
printf("Red Alert: City %d is lost!\n",x-1);
}
else{
printf("City %d is lost.\n",x-1);
}
count = y+1;
}
if(flag){
cout<<"Game Over.";
}
}
int main(){
solve();
return 0;
}
并查集的写法
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int v[510];//并查集数组
int per[510];//记录被攻占的城市
struct Edge
{
int a,b;
}e[5005];
int find(int t)//并查集查找函数
{
return v[t]==t?t:v[t]=find(v[t]);
}
void Union(int x,int y) //并查集连通函数
{
int p1=find(x);
int p2=find(y);
if(p1!=p2)
v[p1]=p2;
}
int count(int z) //通过并查集查找连通分支数
{
int counts=0;
for(int i=0;i<z;i++)
if(v[i]==i)
counts++;
return counts;
}
void INI() //初始化并查集
{
for(int i=0;i<510;i++)
v[i]=i;
}
int main()
{
INI();
memset(per,0,sizeof(per));
int n,m,k;
cin>>n>>m;
for(int i=0;i<m;i++)
{
scanf("%d%d",&e[i].a,&e[i].b);
Union(e[i].a,e[i].b);
}
int c1=count(n);
cin>>k;
for(int j=0;j<k;j++)
{
int c2=0;
int p;
cin>>p;
per[p]=1; //记录被攻占的城市序号
INI(); //每一次都重新初始化并查集
for(int i=0;i<m;i++)
{
if(per[e[i].a]==1||per[e[i].b]==1) //如果该城市已被攻占 就跳过
continue;
Union(e[i].a,e[i].b); //连通两个城市
}
c2=count(n); //每删除一条边,就重新统计连通分支数
if(c1+1 == c2||c1 ==c2) //如果一个城市被攻占后,连通分量增加一个或者不变,表面攻占的城市并不是红色警戒
printf("City %d is lost.\n",p);
else
printf("Red Alert: City %d is lost!\n",p);
c1 = c2; //更新连通每次的连通分支数 ,(注意2)
}
c1=count(n); //最后统计连通分量 若等于城市数,则表面所有城市已被攻占
if(c1==n)
printf("Game Over.\n");
return 0;
}