Pagini recente » Cod sursa (job #2797254) | Cod sursa (job #3187917) | Cod sursa (job #2533681) | Cod sursa (job #108491) | Cod sursa (job #2803834)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
const string msj[] = {"NU", "DA"};
const int DIM = 100005;
int n;
int tata[DIM];
int rad(int nod){
if(nod == tata[nod])
return nod;
return tata[nod] = rad(tata[nod]);
}
void join(int x, int y){
int rx = rad(x);
int ry = rad(y);
tata[ry] = rx;
}
bool check(int x, int y){
return rad(x) == rad(y);
}
int main (){
fin>>n;
for(int i=1; i<=n; i++)
tata[i] = i;
int op, x, y, query_cnt; fin>>query_cnt;
while(query_cnt--){
fin>>op>>x>>y;
if(op == 1)
join(x, y);
else
fout<<msj[check(x, y)]<<"\n";
}
return 0;
}