Cod sursa(job #3224896)

Utilizator leelcheeseCiovnicu Denis leelcheese Data 16 aprilie 2024 14:51:37
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <bits/stdc++.h>
#include <unordered_map>
#define nmax 100007
#define MOD 9901
#define INF 2012345678
#define ll long long
using namespace std;
//#define fin cin
//#define fout cout

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

int n;
int t[nmax];

int FindRoot(int x)
{
	int y, rad;
	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, Q;
	fin >> n >> Q;
	while (Q--)
	{
		fin >> task >> x >> y;
		x = FindRoot(x);
		y = FindRoot(y);
		if (task == 1)
			Union(x, y);
		else if (x != y)
			fout << "NU\n";
		else
			fout << "DA\n";
	}
	fout.close();
	fin.close();
	return 0;
}