Cod sursa(job #3253760)

Utilizator robert_dumitruDumitru Robert Ionut robert_dumitru Data 4 noiembrie 2024 18:44:36
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.56 kb
#include <bits/stdc++.h>
using namespace std;

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

int n, m;
int t[100005];

int FindRoot(int x)
{
	int y, rad = x;
	while (t[rad] != 0)
		rad = t[rad];

	while (x != rad)
	{
		y = t[x];
		t[x] = rad;
		x = y;
	}
	return rad;
}
void Union(int x, int y)
{
	t[y] = x;
}

int main()
{
	int task, x, y;

	fin >> n >> m;
	while (m--)
	{
		fin >> task >> x >> y;
		x = FindRoot(x); y = FindRoot(y);
		if (task == 1) Union(x, y);
		else fout << ((x == y) ? "DA\n" : "NU\n");
	}
}