Pagini recente » Cod sursa (job #3031243) | Cod sursa (job #559828) | Cod sursa (job #873479) | Cod sursa (job #1380868) | Cod sursa (job #2485841)
#include <bits/stdc++.h>
#define Nmax 100005
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int N, M;
int father[Nmax], h[Nmax];
int root(int node) {
int ans;
while (node) {
ans = node;
node = father[node];
}
return ans;
}
int main()
{
f >> N >> M;
for (int m = 1; m <= M; ++m) {
int tip, x, y;
f >> tip >> x >> y;
if (tip == 1) {
int rx = root(x), ry = root(y);
if (h[rx] >= h[ry]) father[ry] = rx;
else father[rx] = ry;
if (h[rx] == h[ry]) ++h[rx];
}
else {
if (root(x) == root(y)) g << "DA\n";
else g << "NU\n";
}
}
return 0;
}