Cod sursa(job #1923075)

Utilizator al_k_ponyClaudiu Babin al_k_pony Data 10 martie 2017 20:35:42
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
# include <bits/stdc++.h>
# define maxn 500001
# define ll long long
# define clock (clock() * 1000.0 / CLOCKS_PER_SEC)
# define rc(s) return cout << s,0
# define _ ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
# define pb push_back
# define mp make_pair
//# define int ll
using namespace std;
const int inf = INT_MAX;

int n,m,x,y,z,p[100001];

int f(int nod)
{
	if(p[nod] == nod) return nod;
    return f(p[nod]);
}

int unite(int x,int y)
{
	p[f(x)] = f(y);
}

int32_t main(){_
    freopen("disjoint.in","r",stdin);
    freopen("disjoint.out","w",stdout);
    cin >> n >> m;
    for(int i = 1;i<=n;i++) p[i] = i;
	while(m--)
	{
		cin >> x >> y >> z;
		if(x == 1)
			unite(y,z);
		else cout << (f(y) == f(z) ? "DA" : "NU") << '\n';
	}
	return 0;
}