Pagini recente » Cod sursa (job #2752522) | Cod sursa (job #2515242) | Cod sursa (job #1281309) | Cod sursa (job #2903242) | Cod sursa (job #3190687)
#include <bits/stdc++.h>
#define DIM 100000
//#define int long long
using namespace std;
//ifstream f("in.in");
//ofstream g("out.out");
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n,m;
int c,x,y;
int len[DIM+5];
int t[DIM+5];
int getRoot(int x){
if(t[x] == x){
return x;
}
return getRoot(t[x]);
}
void join(int x,int y){
x = getRoot(x);
y = getRoot(y);
if(len[x] > len[y]){
swap(x,y);
}
t[x] = y;
len[y] += len[x];
}
signed main(){
f>>n>>m;
for(int i=1;i<=n;i++){
t[i] = i;
len[i] = 1;
}
for(int i=1;i<=m;i++){
f>>c>>x>>y;
if(c==1){
join(x,y);
}else{
if(getRoot(x) == getRoot(y)){
g<<"DA\n";
}else{
g<<"NU\n";
}
}
}
return 0;
}