Pagini recente » Cod sursa (job #96570) | Cod sursa (job #1085268) | Cod sursa (job #2184043) | Rating Hancu Robert (Salamander) | Cod sursa (job #2492215)
#include <bits/stdc++.h>
using namespace std;
int boss[100001];
int nr[100001];
int root(int x){
if(boss[x] == x){
return x;
}
boss[x] = root(boss[x]);
return boss[x];
}
void unite(int a,int b){
a = root(a);
b = root(b);
if(nr[a] > nr[b]){
nr[a] += nr[b];
boss[b] = a;
}else{
nr[b] += nr[a];
boss[a] = b;
}
}
int main()
{
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int i,n,m;
cin >> n >> m;
for(i = 1;i <= n;i++){
boss[i] = i;
nr[i] = 1;
}
while(m--){
int tip,a,b;
cin >> tip >> a >> b;
if(tip == 1){
unite(root(a),root(b));
}else{
if(root(a) != root(b)){
cout << "NU\n";
}else{
cout << "DA\n";
}
}
}
return 0;
}