HDU 1272 小希的迷宫 基础并查集★

https://blog.csdn.net/FJUT_ACM_cjt152/article/details/53018059

代码如下:

#include<bits\stdc++.h>
using namespace std;
int Father[1000];
int fg = 1;
int FindFather(int x)
{
	return Father[x] < 0 ? x : FindFather(Father[x]);
}
void Umerge(int x, int y)
{
	int fx = FindFather(x);//fx,返回-1,或者1-的正数
	int fy = FindFather(y);
	if (fx != fy)
	{
		Father[fy] += Father[fx];//father[X]<0时,存的是孩子的个数,大于0是上个节点数。
		Father[fx] = fy;
	}
	else
	{
		if (Father[x] > 0 && Father[y] > 0)//在一个father下,还要链接
		{
			fg = 0;
		}
	}
}
int main()
{
		memset(Father, -1, sizeof(Father));
		int u, v;
		while (cin>>u>>v)
		{
			if (u == 0 && v == 0) break;
			Umerge(u, v);
		}
		fg ? printf("yes\n") : printf("no");
}

猜你喜欢

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