Pagini recente » Cod sursa (job #2884182) | Cod sursa (job #510463) | Cod sursa (job #909971) | Cod sursa (job #660118) | Cod sursa (job #1944134)
#include<stdio.h>
#include<iostream>
#include<vector>
#define v_i vector<int>
#define v_ip vector<int>*
using namespace std;
int n, m;
int type, x, y;
void unify(v_ip t, v_ip r,int x,int y)
{
if (r->at(x) < r->at(y))
{
t->at(x) = y;
}
else
{
t->at(y) = x;
}
if (r->at(x) == r->at(y))
{
r->at(x)++;
}
}
int found(v_ip t, int x)
{
int rad;
for (rad = x; rad != t->at(rad); rad = t->at(rad));
while (x != t->at(x))
{
int aux = t->at(x);
t->at(x) = rad;
x = aux;
}
return rad;
}
void initialize(v_ip t,v_ip r)
{
for (int i = 1; i <=n; i++)
{
t->at(i) = i;
r->at(i) = 1;
}
}
int main()
{
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
scanf("%d %d", &n, &m);
v_i t(n + 1);
v_i r(n + 1);
initialize(&t,&r);
for (int i = 1; i <= m; i++)
{
scanf("%d %d %d", &type, &x, &y);
if (type == 1)
{
unify(&t,&r, x, y);
}
else
{
if (found(&t, x) == found(&t, y))
{
printf("DA\n");
}
else
{
printf("NU\n");
}
}
}
return 0;
}