PTA-L2-013 Red Alert (25 points)

PTA-L2-013 Red Alert (25 points)

Portal

This question, and look up.

Judging how many connected areas there are is actually judging how many root nodes there are in the union search operation.
It is the operation cnt of the statistical root node.
We should initially save every connected edge.
In the subsequent capture of the city, we only need not to deal with the connected edges of the capture city. Record with vis[].
Others continue to reuse the merge operation to count how many root nodes sum there are.
Judging whether the two numbers are equal/difference 1 (the reason is that after capturing a city, that city will disappear, which is not included in our connectivity, but it will be counted when the number of root nodes is collected, so the difference is 1) because in the
title It is mentioned that if the city of a country is split into k regions, and the loss of one city does not affect the connectivity of other cities, no alarm will be issued. And this is the case for city 2 in the example, losing it does not affect the connectivity of other cities (because city 2 is an isolated individual),
so there will be an equal judgment.
And then determine what the output is.
Then update our cnt, because the title means whether the connectivity has been changed in the previous operation, not the original connectivity.
Finally, judge whether our cnt/sum is n. If it is n, it means that all cities are isolated individuals, which means that all cities have fallen. corresponding to the output.

code part:

#include <bits/stdc++.h>
#define mst(a, n) memset(a, n, sizeof(a))
using namespace std;
const int N = 5e3 + 10;
const int M = 55;
const int INF = 1e6 + 10;
typedef long long ll;

struct node
{
    
    
	int u;
	int v;
}e[N];
int n, m;
int fa[N];
int vis[N];

int find(int x)
{
    
    
	if (x != fa[x])
	{
    
    
		return fa[x] = find(fa[x]);
	}
	return fa[x];
}

void merge(int x, int y)
{
    
    
	int fax = find(x);
	int fay = find(y);
	if (fax != fay)
	{
    
    
		fa[fay] = fax;
	}
}

int main()
{
    
    
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
    
    
		fa[i] = i;
	}
	for (int i = 0; i < m; i++)
	{
    
    
		int x, y;
		scanf ("%d%d", &x, &y);
		e[i].u = x;
		e[i].v = y;
		merge(x, y);
	}
	int cnt = 0;
	for (int i = 0; i < n; i++)
	{
    
    
		if (find(i) == i)
		{
    
    
			cnt++;
		}
	}
	int k;
	int sum = 0;
	cin >> k;
	while (k--)
	{
    
    
		int x;
		cin >> x;
		vis[x] = 1;
		for (int i = 0; i < n; i++)
		{
    
    
			fa[i] = i;
		}
		for (int i = 0; i < m; i++)
		{
    
    
			if (vis[e[i].u] || vis[e[i].v])
			{
    
    
				continue;
			}
			else
			{
    
    
				merge(e[i].u, e[i].v);
			}
		}
		sum = 0;
		for (int i = 0; i < n; i++)
		{
    
    
			if (i == find(i))
			{
    
    
				sum++;
			}
		}
		if (sum == cnt + 1 || sum == cnt)
		{
    
    
			printf ("City %d is lost.\n", x);
		}
		else
		{
    
    
			printf ("Red Alert: City %d is lost!\n", x);
		}
		cnt = sum;
	}
	if (sum == n)
	{
    
    
		cout << "Game Over.\n";
	}
	return 0;
} 

Guess you like

Origin blog.csdn.net/qq_44624316/article/details/110202463