Cod sursa(job #3208144)

Utilizator alexlazuLazureanu Alexandru Ioan alexlazu Data 27 februarie 2024 21:26:14
Problema Paduri de multimi disjuncte Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <tuple>
#include <queue>
#define inmat lin >= 1 && lin <= n && col >=1 && col <= m

using namespace std;

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

#define cin in
#define cout out

const int N = 2e5+2000;

int n, q;

int rad[N] , rang[N];

void UNIRE(int x , int y)
{
	if (rang[x] < rang[y])
	{
		rad[x] = y;
	}
	else
		rad[y] = x;
	if (rang[x] == rang[y]) // rang[y] >= rang[x] deci y este mult invingatoare
		rang[y]++;
}

int find(int x)
{
	int R, y;

	//merg in sus pe arbore pana gasesc un nod care pointeaza catre el insusi
	for (R = x; rad[R] != R; R = rad[R]);

	//aplic compresia drumurilor
	for (; rad[x] != x;)
	{
		y = rad[x];
		rad[x] = R;
		x = y;
	}
	return R;
}

int main()
{
	cin >> n >> q;
	for (int i = 1; i <= n; i++)
	{
		rad[i] = i;
		rang[i] = 1;
	}
	for (int i = 1; i <= q; i++)
	{
		int c , x , y;
		cin >> c >>x >> y;
		if (c == 1)
		{
			UNIRE(x, y);
		}
		else
		{
			if (find(x) == find(y))
				cout << "DA";
			else
				cout << "NU";
			cout << '\n';
		}
	}
}