题目:https://www.patest.cn/contests/gplt/L2-013
#include<bits/stdc++.h>
using namespace std;
const int maxn=505;
//思路:把原来有几个连通量求出来,赋为1后有几个求出来,增加的话警告
bool vis[maxn],v[maxn][maxn];
int n;
void dfs(int x){
vis[x]=true;
for(int i=0;i<n;i++){
if(!vis[i]&&v[x][i]==true)
dfs(i);
}
}
int main(){
int m,k;
cin>>n>>m;
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;
v[a][b]=v[b][a]=true;
}
cin>>k;
for(int i=0;i<k;i++){
int tmp,cnt1=0,cnt2=0;
cin>>tmp;
fill(vis,vis+n,false);
for(int j=0;j<n;j++){
if(!vis[j]){
dfs(j);
cnt1++;
}
}
fill(vis,vis+n,false);
vis[tmp]=true; //把自己删除,不可以走这条.
for(int j=0;j<n;j++){
if(!vis[j]){
dfs(j);
cnt2++;
}
}
for(int j=0;j<n;j++)
v[tmp][j]=v[j][tmp]=0;
if(cnt2>cnt1)
printf("Red Alert: City %d is lost!\n",tmp);
else
printf("City %d is lost.\n",tmp);
}
if(k==n)
cout<<"Game Over."<<endl;
return 0;
}
本文介绍了一种通过深度优先搜索(DFS)算法来判断图的联通性的方法,并实现了一个程序,该程序能够读取图的边信息及需要判断的城市编号,输出因某个城市丢失导致的连通性变化。
704

被折叠的 条评论
为什么被折叠?



