Cod sursa(job #1133151)

Utilizator Sirius2001Happy Birthday Sirius2001 Data 4 martie 2014 15:35:26
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
/*
    Keep It Simple!
*/

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include<stdio.h>
#define MaxN 100005

int n,m, Tree[MaxN], Rang[MaxN];

void Init()
{
	for (int i = 1; i <= n; i++)
		Tree[i] = i, Rang[i] = 1;
}

void AddToTree(int a, int b)
{
	if (Rang[a] > Rang[b])
		Tree[b] = a;
	else
		Tree[a] = b;
	if (Rang[a] == Rang[b]) Rang[b]++;
}

int Find_Root(int x)
{
	int Root = x;
	while (Tree[Root] != Root) Root = Tree[Root];
	while (Tree[x] != x)
	{
		int aux = Tree[x];
		Tree[x] = Root;
		x = aux;
	}
	return Root;
}

int main()
{
	freopen("disjoint.in", "r", stdin);
	freopen("disjoint.out", "w", stdout);

	scanf("%d%d", &n, &m);
	Init();

	int type, x, y;
	for (int i = 1; i <= m; i++)
	{
		scanf("%d%d%d", &type, &x, &y);
		if (type == 1)
			AddToTree(Find_Root(x),Find_Root(y));
		else if (type == 2)
		{
			if (Find_Root(x) == Find_Root(y)) printf("DA\n");
			else printf("NU\n");
		}
	}

}