Cod sursa(job #2291041)

Utilizator cezar.plescaCezar Plesca cezar.plesca Data 27 noiembrie 2018 13:39:46
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 kb
#include<stdio.h>
#include<stdlib.h>

#include<queue>
#include<stack>
#include<vector>
#include<list>

#include <fstream>
#include <iostream>

using namespace std;

#define MAXN 100000
#define MAXM 100000

int M,N;

typedef struct nod{
	int parent;
	int height;
}nod;

nod A[MAXN+1];

int findroot(int x,int method){
	int aux=x;
	while(A[x].parent!=0)
		x=A[x].parent;
	if(method==1)
		return x;

	int root=x;
	x=aux;
	while(A[x].parent!=0){
		aux=x;
		x=A[x].parent;
		A[aux].parent=root;
	}
	return root;
}

#define maxim(a,b) a>b?a:b 

int main(){

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

	scanf("%d %d ", &N, &M);
	
	int x,y,cod;
	int rx,ry;
	for(int i=0;i<M;i++){
		scanf("%d %d %d", &cod, &x, &y);
		if(cod==1){
			rx=findroot(x,1);
			ry=findroot(y,1);
			if(A[rx].height<A[ry].height){
				A[rx].parent=ry;
				A[ry].height=maxim(A[ry].height,(A[rx].height+1));
			}
			else{
				A[ry].parent=rx;
				A[rx].height=maxim(A[rx].height,(A[ry].height+1));
			}
		}
		else{
			rx=findroot(x,2);
			ry=findroot(y,2);
			if(rx==ry)
				printf("DA\n");
			else
				printf("NU\n");
		}
	}

	return 0;
}