天梯 - 红色警报(判断去掉一个节点后的连通分支数是否改变)

战争中保持各个城市间的连通性非常重要。本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报。注意:若该国本来就不完全连通,是分裂的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 #include <bits/stdc++.h>
 2 typedef long long LL;
 3 const int INF=0x3f3f3f3f;
 4 const double eps =1e-8;
 5 const int mod=1e9+7;
 6 const int maxn=1e5+10;
 7 using namespace std;
 8 
 9 int G[505][505];
10 int vis[505];
11 int ko[505];
12 int n;
13 
14 void DFS(int u)
15 {
16     for(int i=0;i<n;i++)
17     {
18         if(!ko[i]&&!vis[i]&&G[u][i])
19         {
20             vis[i]=1;
21             DFS(i);
22         }
23     }
24 }
25 
26 int main()
27 {
28     #ifdef DEBUG
29     freopen("sample.txt","r",stdin);
30     #endif
31     
32     int m;
33     scanf("%d %d",&n,&m);
34     for(int i=1;i<=m;i++)
35     {
36         int u,v;
37         scanf("%d %d",&u,&v);
38         G[u][v]=G[v][u]=1;
39     }
40     int T;
41     scanf("%d",&T);
42     int cnt=0;
43     while(T--)
44     {
45         int k;
46         scanf("%d",&k);
47         cnt++;
48         memset(vis,0,sizeof(vis));
49         int num=0;
50         ko[k]=1; vis[k]=1;
51         for(int i=0;i<n;i++)
52         {
53             if(!ko[i]&&!vis[i]&&G[k][i])
54             {
55                 vis[i]=1;
56                 DFS(i);
57                 num++;
58             }
59         }
60         if(num<=1)
61             printf("City %d is lost.\n",k);
62         else if(num>=2)
63             printf("Red Alert: City %d is lost!\n",k);
64         if(cnt==n)
65         {
66             printf("Game Over.\n");
67             break;
68         }
69     }
70     
71     return 0;
72 }

-

猜你喜欢

转载自www.cnblogs.com/jiamian/p/12578765.html