求助
查看原帖
求助
324095
KevTheDev楼主2020/6/5 22:18

rt, 只想要 9090
(现在 00)

#include <iostream>
using namespace std;

#define MAXN 100000
#define ull unsigned long long

ull t, n, a, b, e;
ull fatherE[MAXN + 1], fatherU[MAXN + 1];

void initE(ull size);
ull findE(ull x);
void unionSetE(ull x, ull y);

void initU(ull size);
ull findU(ull x);
void unionSetU(ull x, ull y);

int main()
{
	cin >> t;
	for (ull i = 0; i < t; i++)
	{
		initE(MAXN);
		initU(MAXN);
		cin >> n;
		for (ull j = 0; j < n; j++)
		{
			cin >> a >> b >> e;
			if (e == 1)
				unionSetE(a, b);
			else
				unionSetU(a, b);
		}
		for (ull j = 1; j <= n; j++)
		{
		    if (fatherE[j] == fatherU[j])
			{
				cout << "NO" << endl;
				break;
			}
			cout << "YES" << endl;
		}
	}
	return 0;
}

void initE(ull size)
{
	for (ull i = 1; i <= size; i++)
		fatherE[i] = i;
}

ull findE(ull x)
{
	while (x != fatherE[x])
		x = fatherE[x];
	return fatherE[x];
}

void unionSetE(ull x, ull y)
{
	ull root1 = findE(x);
	ull root2 = findE(y);
	if (root1 == root2)
		return;
	else if (root1 < root2)
		fatherE[root2] = root1;
	else
		fatherE[root1] = root2;
}

void initU(ull size)
{
	for (ull i = 1; i <= size; i++)
		fatherU[i] = i;
}

ull findU(ull x)
{
	while (x != fatherU[x])
		x = fatherU[x];
	return fatherU[x];
}

void unionSetU(ull x, ull y)
{
	ull root1 = findU(x);
	ull root2 = findU(y);
	if (root1 == root2)
		return;
	else if (root1 < root2)
		fatherU[root2] = root1;
	else
		fatherU[root1] = root2;
}
2020/6/5 22:18
加载中...