Pagini recente » Cod sursa (job #2968796) | Rating Tanase Emanuel (infont) | Cod sursa (job #992839) | Cod sursa (job #3251668) | Cod sursa (job #2291041)
#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;
}