Pagini recente » Cod sursa (job #250877) | Borderou de evaluare (job #2772022) | Cod sursa (job #2158211) | Cod sursa (job #2121971) | Cod sursa (job #2838887)
#include <bits/stdc++.h>
using namespace std;
#define l_vec 100001
// ifstream fin("cod.in");
// ofstream fout("cod.out");
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int det_rad(int nod);
void unim(int nod1, int nod2);
void inclus(int nod1, int nod2);
int par[l_vec], rads[l_vec];
int nr_mul, nr_op;
int main()
{
fin >> nr_mul >> nr_op;
int op, n1, n2;
for (int i = 1; i <= nr_op; i++)
{
fin >> op >> n1 >> n2;
if (op == 1)
unim(n1, n2);
else
inclus(n1, n2);
}
return 0;
}
void unim(int nod1, int nod2)
{
int root1, root2;
root1 = det_rad(nod1);
root2 = det_rad(nod2);
if (root1 == root2)
return;
if (rads[root1] >= rads[root2])
rads[root1] += rads[root2] + 1, par[root2] = root1;
else
rads[root2] += rads[root1] + 1, par[root1] = root2;
}
int det_rad(int nod)
{
int temp = nod;
while (par[nod] != 0)
nod = par[nod];
int next = temp;
while (par[next] != 0)
{
next = par[temp];
par[temp] = nod;
}
return nod;
}
void inclus(int nod1, int nod2)
{
if (det_rad(nod1) == det_rad(nod2))
{
fout << "DA\n";
return;
}
fout << "NU\n";
}