Pagini recente » Cod sursa (job #756904) | Cod sursa (job #1578711) | Cod sursa (job #2159112) | Cod sursa (job #1496480) | Cod sursa (job #2533720)
#include<iostream>
#include<fstream>
using namespace std;
const int NMAX = 100005;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int t[NMAX], h[NMAX];
int tx(int x){
while(t[x] != x){
x = t[x];
}
return x;
}
void unionset(int x, int y){
if(h[x] == h[y])
h[x]++,t[y]=x;
else{
if(h[x] > h[y])
t[y]=x;
else
t[x]=y;
}
}
int main(){
int n,m;
in>>n>>m;
for(int i=1;i<=n;i++){
t[i]=i;
h[i]=1;
}
for(int i=1;i<=m;i++){
int p;
in>>p;
if(p == 1){
int a,b;
in>>a>>b;
unionset(tx(a),tx(b));
}
if(p == 2){
int a,b;
in>>a>>b;
if(tx(a) == tx(b))
out<<"DA"<<'\n';
else
out<<"NU"<<'\n';
}
}
return 0;
}