Cod sursa(job #3280631)

Utilizator tomavladnicolae@gmail.comTomavlad [email protected] Data 26 februarie 2025 20:52:27
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.61 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, m, t[100005];

int Find(int x)
{
	int r = x, y;

	while (t[r] != 0)
		r = t[r];

	while (x != r)
	{
		y = t[x];
		t[x] = r;
		x = y;
	}

	return r;
}

void Union(int x, int y)
{
	t[y] = x;
}

int main()
{
	int i, op, x, y;
	fin >> n >> m;
	while (m--)
	{
		fin >> op >> x >> y;
		if (op == 1)
		{
			x = Find(x);
			y = Find(y);
			if (x != y)
				Union(x, y);
		}
		else if (Find(x) == Find(y))
			fout << "DA\n";
		else fout << "NU\n";
	}

	return 0;
}