Pagini recente » Cod sursa (job #2586219) | Cod sursa (job #581226) | Cod sursa (job #21665) | Cod sursa (job #185086) | Cod sursa (job #1497031)
#include <bits/stdc++.h>
using namespace std;
int v[100005];
int lg[100005];
int first(int a) {
a = v[a];
while(a != v[a])
a = v[a];
return a;
}
int bun(int a, int b) {
int A = first(a);
int B = first(b);
return (A == B);
}
void mergeList(int a, int b) {
int A = first(a);
int B = first(b);
if(lg[A] <= lg[B])
int aux = A, A = B, B = aux;
v[B] = v[A];
if(lg[A] == lg[B])
lg[A] ++;
}
int main()
{
ios::sync_with_stdio(false);
ifstream cn("disjoint.in");
ofstream co("disjoint.out");
int n, m;
int x, y, t;
cn >> n >> m;
for(int i = 1; i <= n; i ++) {
v[i] = i;
lg[i] = 1;
}
for(int i = 1; i <= m; i ++) {
cn >> t >> x >> y;
if(t == 1) mergeList(x, y);
else {
if(bun(x, y))
co << "DA\n";
else
co << "NU\n";
}
}
cn.close();
co.close();
return 0;
}