Cod sursa(job #2568735)

Utilizator mihai50000Mihai-Cristian Popescu mihai50000 Data 4 martie 2020 09:39:28
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda r3capitusulare Marime 0.68 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

const int DIM = 1e5 + 1;

int siz[DIM];
int to[DIM];

int find(int nod)
{
	if(to[nod] == nod)
		return nod;
	
	return (to[nod] = find(to[nod]));
}

void unite(int x, int y)
{
	x = find(x);
	y = find(y);
	
	if(siz[x] < siz[y])
		swap(x, y);
		
	to[y] = x;
	siz[x] += siz[y];
}

main()
{
	int n, m;
	fin >> n >> m;
	
	for(int i = 1; i <= n; ++i)
	{
		to[i] = i;
		siz[i] = 1;
	}
	
	for(; m; --m)
	{
		int op, x, y;
		fin >> op >> x >> y;
		
		if(op == 1)
		{
			unite(x, y);
		}
		else
		{
			if(find(x) != find(y))
			{
				fout << "NU\n";
			}
			else
			{
				fout << "DA\n";
			}
		}
	}
}