Cod sursa(job #2036481)

Utilizator zacuscaAlex Iordache zacusca Data 10 octombrie 2017 19:01:35
Problema Paduri de multimi disjuncte Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 0.96 kb
// paduri de multimi disjuncte.cpp : Defines the entry point for the console application.
//

#include <stdlib.h>
#include <stdio.h>
#define MAX_SIZE 100003

int N, M, type, x, y;
int rank[MAX_SIZE], father[MAX_SIZE];

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

inline void join(int x, int y)
{
	if (rank[x] < rank[y])
	{
		father[x] = y;
	}
	else
	{
		father[y] = x;
	}
	if (rank[x] == rank[y])
	{
		++rank[y];
	}
}

int main()
{
	freopen("disjoint.in", "r", stdin);
	
	scanf("%d %d", &N, &M);

	for (int i = 1; i <= N; ++i)
	{
		rank[i] = 1;
		father[i] = i;
	}


	freopen("disjoint.out", "w", stdout);

	//int type, x, y;
	while (M--)
	{
		scanf("%d %d %d", &type, &x, &y);
		int rx = find(x), ry = find(y);
		if (type == 1)
		{
			join(rx, ry);
		}
		else
		{
			printf ( (rx == ry ? "DA\n" : "NU\n") );
			
		}
	}

	fclose(stdin);
	fclose(stdout);

	return 0;
	
}