Cod sursa(job #1810754)

Utilizator DoubleNyNinicu Cristian DoubleNy Data 20 noiembrie 2016 15:24:23
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <bits/stdc++.h>
#define ll long long
#define lli long long int
#define endl "\n"

using namespace std;

int t[100001],h[100001];

void unionf(int x, int y){
	if(h[x] > h[y])
			t[y] = x;
	else 
			t[x] = y;
	if(h[x] == h[y])
		h[y]++;
}

int find(int x){
		while(t[x] != x){
				x = t[x];
		}
		return(x);
}

int main(){
	ios_base::sync_with_stdio(false);
        cin.tie(NULL);
    	//
	ifstream cin("disjoint.in");
	ofstream cout("disjoint.in");
		int n,m;
		cin >> n >> m;
		for(int i=1;i<=n;i++)
			t[i] = i;
		for(int i=0;i<m;i++){	
				int tp, a, b;
				cin >> tp >> a >> b;
				if(tp == 1){
							int root0, root1;
							root0 = find(a);
							root1 = find(b);
							if(root0 != root1)
								unionf(root0, root1);
				} else {
							int root0, root1;
							root0 = find(a);
							root1 = find(b);
							if(root0 == root1)
								cout << "DA" << endl;
							else
								cout << "NU" << endl;
				}
		}
    return(0);
}