Cod sursa(job #1966652)

Utilizator brczBereczki Norbert Cristian brcz Data 15 aprilie 2017 14:26:56
Problema Paduri de multimi disjuncte Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include<bits/stdc++.h>

#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
#define sz size
#define pb push_back
#define mp make_pair
#define bg begin
#define nd end
using namespace std;

const int maxn = 100003;

int n,m;


int parent[maxn], RG[maxn];

int find(int x)
{
    int R, y;
 
    for (R = x; parent[R] != R; R = parent[R]);
 
    for (; parent[x] != x;)
    {
        y = parent[x];
       	parent[x] = R;
        x = y;
    }
    return R;
}
 
void unite(int x, int y)
{
    if (RG[x] > RG[y])
        parent[y] = x;
            else parent[x] = y;
 
    if (RG[x] == RG[y]) RG[y]++;
}

void init(){
	for (int i = 1; i <= n; i++)
    	{
    	    parent[i] = i;
    	    RG[i] = 1;
    	}
 
}

int main(){

	#ifndef ONLINE_JUDGE
	freopen("disjoint.in","r",stdin);
	freopen("disjoint.out","w",stdout);
	#endif


	// ios_base::sync_with_stdio(false);

	cin >> n >> m;
	int cd,x,y;
	init();
	for(int i=1;i<=m;i++){
		cin >> cd >> x >> y;

		if(cd == 2){
			if(find(x) == find(y)){
				cout << "DA" << '\n';
			}
			else{
				cout << "NU" << '\n';
			}
		}
		else{
			if(find(x) == find(y)){
			}
			unite(find(x),find(y));
		}
	}


	return 0;
}