Pagini recente » Cod sursa (job #2378130) | Cod sursa (job #582299) | Cod sursa (job #1942311) | Cod sursa (job #600496) | Cod sursa (job #1985999)
#include <fstream>
#include <cassert>
#include <iostream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
const int NMAX = 100000;
const int DIM = 1000000;
int n, m, poz;
int sef[1 + NMAX]; // each elemeth's boss
int ssz[1 + NMAX]; // each size
char buff[DIM];
int read(){
int nr = 0;
while(!isdigit(buff[poz])){
if(++poz == DIM){
in.read(buff, DIM);
poz = 0;
}
}
while(isdigit(buff[poz])){
nr = nr * 10 + (buff[poz] - '0');
if(++poz == DIM){
in.read(buff, DIM);
poz = 0;
}
}
return nr;
}
int getsef(int el){
if(sef[el] == el)
return el;
else{
sef[el] = getsef(sef[el]);
return sef[el];
}
}
int main()
{
//in >> n >> m;
n = read();
m = read();
for(int i = 1; i <= n; i++){
sef[i] = i;
ssz[i] = 1;
}
for(int i = 1; i <= m; i++){
int p, x, y;
//in >> p >> x >> y;
p = read();
x = read();
y = read();
if(p == 1){
int bx = getsef(x);
int by = getsef(y);
assert(bx != by);
if(ssz[by] <= ssz[bx]){
sef[by] = bx;
ssz[bx] += ssz[by];
}else{
sef[bx] = by;
ssz[by] += ssz[bx];
}
}else if(p == 2){
if(getsef(x) == getsef(y))
out << "DA\n";
else
out << "NU\n";
}
}
in.close();
out.close();
return 0;
}