red alert

L2-013. Red Alert

time limit
400 ms
memory limit
65536 kB
code length limit
8000 B
Judgment procedure
Standard
author
Chen Yue

It is important to maintain connectivity between cities during a war. This question asks you to write an alert program that will sound a red alert when the loss of a city causes the country to be divided into multiple unconnected regions. Note: If the country is inherently not fully connected, it is split k regions, and losing a city does not change the connectivity between other cities, don't raise an alarm.

Input format:

The input gives two integers N (0 < N <=500) and M (<=5000) in the first line, which are the number of cities (so the default cities are numbered from 0 to N-1) and the path connecting the two cities number of bars. After M lines, each line gives the number of the two cities connected by a path, separated by 1 space. The captured information is given after the city information, that is, a positive integer K followed by the numbers of the K captured cities.

Note: The input guarantees that the captured city numbers given are valid and without duplication, but it does not guarantee that the given access route is not duplicated.

Output format:

For each captured city, if it would change the connectivity of the entire country, output "Red Alert: City k is lost!", where k is the number of the city; otherwise just output "City k is lost." Can. If the country loses its last city, add a line to output "Game Over.".

Input sample:
5 4
0 1
1 3
3 0
0 4
5
1 2 0 4 3
Sample output:
City 1 is lost.
City 2 is lost.
Red Alert: City 0 is lost!
City 4 is lost.
City 3 is lost.
Game Over.



# include <iostream>
# include <numeric>
# include <algorithm>
# include <functional>
# include <list>
# include <map>
# include <set>
# include <stack>
# include <deque>
# include <queue>
# include <vector>
# include <ctime>
# include <cstdlib>
# include <cmath>
# include <string>
# include <cstring>

using namespace std;

int pre[505];

struct node
{
	int x, y;
};


void fun()
{
	for(int i = 0; i < 505; i++)
	{
		pre[i] = i;
	}
}

int Find(int x)
{
	if(x != pre[x])
	{
		pre[x] = Find(pre[x]);
	}
	return pre[x];
}

void join(int x, int y)
{
	int fx = Find(x);
	int fy = Find(y);
	if(fx != fy)
	{
		pre[fy] = fx;
	}
}

int main(int argc, char *argv[])
{
	int n, m;
	cin >> n >> m;

		fun();
		struct node s[5005];
		for(int i = 0; i < m; i++)
		{
			cin >> s[i].x >> s[i].y;
			join(s[i].x, s[i].y);
		}
		int k;
		cin >> k;
		int lost[505] = {0};
		int c1 = 0;
		for(int i = 0; i < n; i++)
		{
			if(pre[i] == i)
			{
				c1++;
			}
		}
		int i;
		for(i = 0; i < k; i++)
		{
			int city;
			cin >> city;
			// each initialization
			fun();
			int c2 = 0;
			lost[city] = 1;
			for(int j = 0; j < m; j++)
			{
				//Because it is a merge, it is required to point to and be pointed to to be 0.
				if(!lost[s[j].x] && !lost[s[j].y])
				{
					join(s[j].x, s[j].y);
				}
			}
			//Calculate the number of city root nodes after updating the data
			for(int p = 0;  p < n; p++)
			{
				if(!lost[p] && pre[p] == p)
				{
					c2++;
				}
			}

			if(c1 == c2 || c1 == c2 + 1)
			{
				printf("City %d is lost.\n",city);
			}
			else
			{
				printf("Red Alert: City %d is lost!\n",city);
			}
			c1 = c2;
		}
		if(k == n)
		{
				printf("Game Over.\n");
		}
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326003896&siteId=291194637