Pagini recente » Cod sursa (job #2184523) | Cod sursa (job #1248335) | Cod sursa (job #2834195) | Cod sursa (job #2691349) | Cod sursa (job #2312133)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
#define NMax 100005
int n, m;
int tata[NMax], h[NMax];
int Find(int);
void Union(int, int);
int main(){
int op, x, y;
fin >> n >> m;
while(m--){
fin >> op >> x >> y;
if(op == 1) Union(x, y);
else if(Find(x) == Find(y)) fout << "DA\n";
else fout << "NU\n";
}
}
void Union(int x, int y){
x = Find(x);
y = Find(y);
if(h[x] > h[y])
tata[y] = x;
else{
tata[x] = y;
if(h[x] == h[y]) h[y]++;
}
}
int Find(int x){
int aux, p = x;
while(tata[p]) p = tata[p];
while(x != p){
aux = tata[x];
tata[x] = p;
x = aux;
}
return p;
}