Cod sursa(job #2933461)

Utilizator gabriel10tm@gmail.comGabriel Marian [email protected] Data 5 noiembrie 2022 10:59:52
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.42 kb
/*
https://www.spoj.com/problems/WATER/
*/

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pi = pair<int, int>;
using ti = tuple<int, int, int>;
using tl = tuple<ll, ll, ll>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pi>;
using vpil = vector<pil>;
using vpli = vector<pli>;
using vpl = vector<pl>;
using vti = vector<ti>;
using vtl = vector<tl>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vvpi = vector<vpi>;
using vvpil = vector<vpil>;
using vvpli = vector<vpli>;
using vvpl = vector<vpl>;
using vvti = vector<vti>;
using vvtl = vector<vtl>;


#if 1
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
#define cin fin
#define cout fout
#endif
const int nmx = 1e6 + 1;
int n, m;
int t[nmx],gr[nmx];


int rad(int k)
{
	if (t[k] == 0)
		return k;
	int it = k;
	while (t[it])
		it = t[it];
	t[k] = it;
	return it;
}



void unite(int n1, int n2)
{
	int t1 = rad(n1), t2 = rad(n2);
	if (t1 != t2)
	{
		if (gr[n1] == gr[n2])
		{
			gr[n1]++;
			t[t2] = t1;
		}
		else if (gr[n1] < gr[n2])
			t[t1] = t2;
		else
			t[t2] = t1;
	}
}

int main()
{
	cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		int op, x, y;
		cin >> op >> x >> y;
		if (op == 1)
			unite(x, y);
		else 
			cout << (rad(x) == rad(y) ? "DA" : "NU") << "\n";
	}
}