Cod sursa(job #1437438)

Utilizator StefanRARapeanu-Andreescu Stefan StefanRA Data 17 mai 2015 19:02:08
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.66 kb
#include <fstream>
#define MAXN 100001
int n, m, code, x, y, darth_vader[MAXN];
void join_the_darkside(int x, int y)
{
	while (darth_vader[x])
		x=darth_vader[x];
	while (darth_vader[y])
		y=darth_vader[y];
	darth_vader[y]=x;
}
bool same_shit(int x, int y)
{
	while (darth_vader[x])
		x=darth_vader[x];
	while (darth_vader[y])
		y=darth_vader[y];
	return x==y;
}
int main()
{
	freopen("disjoint.in", "r", stdin);
	freopen("disjoint.out", "w", stdout);
	scanf("%d %d", &n, &m);
	while (m--)
	{
		scanf("%d %d %d", &code, &x, &y);
		if (code==1)
			join_the_darkside(x, y);
		else
			printf((same_shit(x, y))?"DA\n":"NU\n");
	}
	return 0;
}