l2-013. Red AlertTime limit MS
Memory Limit 65536 KB
Code length limit 8000 B
Standard author Chen Yue The procedure of the sentence
It is important to maintain connectivity between cities in war. The subject requires you to write a warning program that emits a red alert when the loss of a city causes the country to be divided into inaccessible areas. Note: If the country is not fully connected, it is a division of the K region, and the loss of a city does not change the connectivity between other cities, do not send an alarm.
Input Format:
The input gives two integers N (0 < N <=500) and M (<=5000) in the first row, the number of cities (hence the default city from 0 to N-1 numbers) and access bars connecting two cities. Then line m, each row gives the number of two cities connected by a path, separated by 1 spaces. After the city information is given the captured information, i.e. a positive integer k and the number of the subsequent K-Captured cities.
Note: The input guaranteed to the captured city numbers is legal and does not duplicate, but does not guarantee that the given path is not duplicated.
output Format:
For each captured city, if it would change the connectivity of the entire country, the output "Red alert:city K is lost!", where K is the number of the city, otherwise only "urban K is lost." Can. If the country loses its last city, it adds a line of output "Game over." Input Sample:
5 4
0 1
1 3
3
0
0 4 5 1 2 0-4 3
Output Sample:
City 1 is lost.
City 2 is lost.
Red alert:city 0 is lost!
City 4 is lost.
City 3 is lost.
Game over.
This problem is the first idea is to check the set but feel to update many times feel will time out, but after the game found is really naked and check the set ...
#include <iostream> #include <bits/stdc++.h> using namespace std;
const int N = 510;
int f[n], visit[n];
struct node {int x, y;} p[5100];
void Merg (int x,int y);
int find (int x);
int main () {int n, m;
scanf ("%d%d", &n, &m);
for (int i=0; i<n; i++) {f[i]=i;
for (int i=0; i<m; i++) {int x, y;
scanf ("%d%d", &x, &y);
P[i].x=x,p[i].y=y;
Merg (X,y);
int num=0, NUM1;
for (int i=0; i<n; i++) {if (f[i]==i) {num++;
} memset (Visit,0,sizeof (visit));
int k;
scanf ("%d", &k);
while (k--) {num1=0;
for (int i=0; i<n; i++) {f[i]=i;
int x;
scanf ("%d", &x);
Visit[x]=1; for (int i=0; i<m; i++) {if (visit[p[i].x]==1| |
Visit[p[i].y]==1) {continue;
} else {Merg (P[I].X,P[I].Y);
for (int i=0; i<n; i++) {if (f[i]==i) {num1++; } if (num==num1| |
NUM+1==NUM1) {printf ("City%d is lost.\n", x);
else {printf ("Red alert:city%d is lost!\n", x);
} num=num1;
} num=0;
for (int i=0;i<n;i++) {if (visit[i]==1) {num++;
} if (num==n) {printf ("Game over.\n");
return 0;
} void Merg (int x,int y) {int t1=find (x);
int T2=find (y);
if (t1!=t2) {f[t2]=t1;
} return;
int find (int x) {if (X==f[x]) {return f[x];
else {f[x]=find (f[x]);
return f[x]; }
}