Pagini recente » Cod sursa (job #254274) | Cod sursa (job #318602) | Cod sursa (job #256026) | Cod sursa (job #1647061) | Cod sursa (job #2231476)
#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include <cstdio>
using namespace std;
const int nmax = 100005;
int n, m, op, x, y;
class Disjoint {
private:
int mx;
int parent[nmax];
int rnk[nmax];
public:
Disjoint(int n) : mx(n) {
for (int i = 0; i < mx; i++) {
parent[i] = i;
rnk[i] = 0;
}
}
void Uneste(int x, int y) {
if(rnk[y] >= rnk[x])
{
if(rnk[y] == rnk[x])
rnk[y]++;
parent[x] = y;
}
else parent[y] = x;
}
int FindSet(int x) {
if (parent[x] != x) {
parent[x] = FindSet(parent[x]);
}
return parent[x];
}
};
int main()
{
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
scanf("%d%d", &n, &m);
Disjoint d(n);
for(;m;m--)
{
scanf("%d%d%d", &op, &x, &y);
x = d.FindSet(x);
y = d.FindSet(y);
if(op == 1)
d.Uneste(x, y);
else {
if(x == y)
printf("DA\n");
else printf("NU\n");
}
}
return 0;
}