Pagini recente » Cod sursa (job #2614040) | Cod sursa (job #190791) | Cod sursa (job #2113305) | Cod sursa (job #2134530) | Cod sursa (job #2291040)
#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;
}