Pagini recente » Cod sursa (job #2741129) | Cod sursa (job #282947) | Cod sursa (job #1031336) | Cod sursa (job #773268) | Cod sursa (job #2816271)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
/**
Paduri de multimi disjuncte!!
(alg. Union-Find)
*/
int n;
int t[100003];
/// unifica c.c de radacini x si y
void Union (int x , int y)
{
t[y] = x;
}
/// ret. radacina c.c din care face parte nodul x
int Find (int x)
{
int rad, y;
rad = x;
while (t[rad] != 0)
rad = t[rad];
/// comprimam drumurile, adica toate nodurile din traseul
/// de la x la rad se leaga direct de radacina
while (x != rad)
{
y = t[x];
t[x] = rad;
x = y;
}
return rad;
}
int main()
{
int y , x , q, op;
fin >> n >> q;
for (int i = 1; i <= q; i++)
{
fin >> op >> x >> y;
x = Find(x);
y = Find(y);
if (op == 1)
{
if (x != y)
Union(x , y);
}
else
{
if (x == y)
fout << "DA\n";
else fout << "NU\n";
}
}
fout.close();
return 0;
}