Pagini recente » Cod sursa (job #1583226) | Cod sursa (job #2432503) | Cod sursa (job #2717734) | Cod sursa (job #3032974) | Cod sursa (job #1771306)
#include <cstring>
#include <cstdio>
#include <iostream>
using namespace std;
int n, m;
int t[100001];
int a[100001];
int radacina(int x)
{
int aux = x, tmp;
while(t[x] != x)
{
x = t[x];
}
while(t[aux] != aux)
{
tmp = aux;
aux = t[aux];
t[tmp] = x;
}
return x;
}
void initializare()
{
for (int i=1; i<=100000; i++)
t[i] = i;
}
void read()
{
int operatie, x, y;
scanf("%d %d\n", &n, &m);
for (int i=1; i<=m; i++)
{
scanf("%d %d %d\n", &operatie, &x, &y);
if (operatie == 1)
{
if (a[x] == a[y])
{
t[radacina(y)] = radacina(x);
a[x]++;
}
else if (a[x] < a[y])
{
t[radacina(x)] = radacina(y);
}
else if (a[x] > a[y])
{
t[radacina(y)] = radacina(x);
}
}
else if (radacina(x) == radacina(y))
cout<<"DA\n";
else
cout<<"NU\n";
}
}
int main()
{
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
initializare();
read();
return 0;
}