Pagini recente » Cod sursa (job #215995) | Cod sursa (job #2860821) | Cod sursa (job #1383094) | Cod sursa (job #2374325) | Cod sursa (job #1944198)
#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 = x;
int aux;
for (rad = x; rad != t->at(rad); rad = t->at(rad));
while (x != rad)
{
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 < t->size(); 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, found(&t, x), found(&t, y));
}
else
{
if (found(&t, x) == found(&t, y))
{
printf("DA\n");
}
else
{
printf("NU\n");
}
}
}
return 0;
}