Cod sursa(job #2000627)

Utilizator stefan_creastaStefan Creasta stefan_creasta Data 14 iulie 2017 11:55:34
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <cstdio>
using namespace std;
const int NMAX = 100005;
int h[NMAX];
int t[NMAX];
int FindSet(int x)
{
	if(t[x] == x)
		return x;
	return FindSet(t[x]);
}
void UnionSet(int x, int y)
{
	if(h[x] == h[y]) {
		++h[x];
		t[y] = x;
	}
	else if(h[x] > h[y]) {
		t[y] = x;
	}
	else {
		t[x] = y;
	}
}

int main()
{
    int n, m, i, x, y, op;
    freopen("disjoint.in","r",stdin);
    freopen("disjoint.out","w",stdout);
    scanf("%d%d", &n, &m);
    for(i = 1;i <= n; ++i) {
		h[i] = 1;
		t[i] = i;
    }
    for(i = 1;i <= m; ++i) {
		scanf("%d%d%d", &op, &x, &y);
		x = FindSet(x);
		y = FindSet(y);
		if(op == 1) {
			UnionSet(x, y);
		}
		else {
			if(x == y) {
				printf("DA\n");
			}
			else {
				printf("NU\n");
			}
		}
    }
    return 0;
}