Cod sursa(job #1944159)

Utilizator vlcmodanModan Valentin vlcmodan Data 28 martie 2017 23:01:05
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include<stdio.h>
#include<iostream>
#include<vector>

#define v_i vector<int>
#define v_ip vector<int>*
using namespace std;


int n, m;
int type, x, y;

void unify(v_ip t, v_ip r,int x,int y)
{
	if (r->at(x) < r->at(y))
	{
		t->at(x) = y;
	}
	else
	{
		t->at(y) = x;
	}

	if (r->at(x) == r->at(y))
	{
		r->at(x)++;
	}
	
	
}

int found(v_ip t, int x)
{
	
	int rad;

	for (rad = x; rad != t->at(rad); rad = t->at(rad));

	while (x != t->at(x))
	{
		int aux = t->at(x);
		t->at(x) = rad;
		x = aux;
	}

	return rad;

}



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

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

	v_i t(100010);
	v_i r(100010);

	

	for (int i = 1; i <= n; ++i)
	{
		t[i] = i;
		r[i] = 1;
	}

	for (int i = 1; i <= m; i++)
	{
		int tip;
		scanf("%d%d%d", &tip, &x, &y);

		if (tip == 1)
		{
			unify(&t,&r, x, y);
		}
		else
		{
			if (found(&t, x) == found(&t, y))
			{
				printf("DA\n");
			}
			else
			{
				printf("NU\n");
			}
		}
	}

	fclose(stdin);
	fclose(stdout);



	return 0;
}