Cod sursa(job #2784145)

Utilizator bubblegumixUdrea Robert bubblegumix Data 15 octombrie 2021 21:50:04
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int lim = 1e5 + 5;
int t[lim];
int sz[lim];
int n, m;
int find(int x)
{
	if (!t[x])
		return x;
	
	return t[x] = find(t[x]);
}
void unite(int x, int y)
{
	int tx = find(x), ty = find(y);
	if (tx != ty)
	{
		if (sz[tx] > sz[ty])
			t[ty] = tx;
		else
		{
			t[tx] = ty;
			if (sz[tx] == sz[ty])
				sz[ty]++;
		}
	}
}
int main()
{
	freopen("disjoin.in", "r", stdin);
	freopen("disjoin.out", "w", stdout);

	cin >> n >> m;
	for (int i = 1; i <= m; i++)
	{
		int op, x, y;
		cin >> op >> x >> y;
		if (op == 1)
			unite(x, y);
		else
		{
			if (find(x) == find(y))
				cout << "DA\n";
			else
				cout << "NU\n";
		}
	}
	
}