More is better (并查集)

#include<bits/stdc++.h>
using namespace std;
int Father[10000];
int FindFather(int x)
{
	return Father[x] < 0 ? x : FindFather(Father[x]);
}
void Umerge(int x, int y)
{
	int Fx = FindFather(x), Fy = FindFather(y);
	if (Fx!= Fy)	
	{
		Father[Fy] += Father[Fx];
		Father[Fx] = Fy;
	}
}
set<int> node;
int main()
{
	int n;
	while (cin >> n)
	{
		memset(Father, -1, sizeof(Father));
		node.clear();
		int u, v;
		for (int i = 0; i < n; i++)
		{
			cin >> u >> v;
			node.insert(u);
			node.insert(v);
			Umerge(u, v);
		}
		int maxnum = 0;
		for (int i : node)
		{
			if (Father[i] < 0) maxnum = max(maxnum, -Father[i]);
		}
		cout << maxnum<<" ";
	}
	
}

猜你喜欢

转载自blog.csdn.net/qq_31741481/article/details/83212392