Pagini recente » Cod sursa (job #138190) | Cod sursa (job #1715616) | Cod sursa (job #570896) | Cod sursa (job #960559) | Cod sursa (job #2701686)
#include<bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int NMAX = 100005;
int tt[NMAX], rang[NMAX];
int n, m, i, j, a, b, operatie;
int find_root (int x) {
int r;
for (r = x; tt[r] != r; r = tt[r]);
for (; tt[x] != x;) {
int y = tt[x];
tt[x] = r;
x = y;
}
return r;
}
void unire (int x, int y) {
if (rang[x] > rang[y])
tt[y] = x;
else
tt[x] = y;
if (rang[x] == rang[y])
rang[y]++;
}
int main(){
fin >> n >> m;
for (i = 1; i <= n; i++) {
rang[i] = 1;
tt[i] = i;
}
for (j = 1; j <= m; j++) {
fin >> operatie >> a >> b;
if (operatie == 1)
unire(find_root(a), find_root(b));
else {
if (find_root(a) == find_root(b))
fout << "DA\n";
else
fout << "NU\n";
}
}
}