Cod sursa(job #2291040)

Utilizator cezar.plescaCezar Plesca cezar.plesca Data 27 noiembrie 2018 13:35:29
Problema Paduri de multimi disjuncte Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.2 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];

ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

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(){

	fin >> N >> M;
	
	int x,y,cod;
	int rx,ry;
	for(int i=0;i<M;i++){
		fin >> 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)
				fout << "DA" << endl;
			else
				fout << "NU" << endl;
		}
	}

	fin.close();
    fout.close();

	return 0;
}