Pagini recente » Cod sursa (job #1540398) | Cod sursa (job #1482147) | Cod sursa (job #1627708) | Cod sursa (job #631598) | Cod sursa (job #1944158)
#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;
}
int main()
{
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
scanf("%d %d", &n, &m);
v_i t(100010);
v_i r(100010);
for (int i = 1; i <= n; ++i)
{
t[i] = i;
r[i] = 1;
}
for (int i = 1; i <= m; i++)
{
int tip;
scanf("%d%d%d", &tip, &x, &y);
if (tip == 1)
{
unify(&t,&r, x, y);
}
else
{
if (found(&t, x) == found(&t, y))
{
printf("DA\n");
}
else
{
printf("NU\n");
}
}
}
fclose(stdin);
fclose(stdout);
return 0;
}