Pagini recente » Cod sursa (job #2399717) | Cod sursa (job #88435) | Cod sursa (job #1140201) | Cod sursa (job #36228) | Cod sursa (job #2816457)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
int t[100001], nr[100001];
//la inceput, nr[i] = 1, pentru orice i
void uneste (int x, int y)
{
if (nr[x] < nr[y])
{
t[x] = y;
nr[y] = nr[y] + nr[x];
}
else
{
t[y] = x;
nr[x] = nr[x] + nr[y];
}
}
int cauta_radacina (int x)
{
int r, y;
r = x;
while (t[r] != 0)
r = t[r];
while (x != r)
{
y = t[x];
t[x] = r;
x = y;
}
return r;
}
int main()
{
int n, m, i, t, x, y, rx, ry;
fin >> n >> m;
for (i = 1; i<=n; i++)
nr[i] = 1;
for (i = 1; i<=m; i++)
{
fin >> t >> x >> y;
if (t == 1)
{
rx = cauta_radacina (x);
ry = cauta_radacina (y);
uneste (rx, ry);
}
else
{
rx = cauta_radacina (x);
ry = cauta_radacina (y);
if (rx == ry)
fout << "DA\n";
else
fout << "NU\n";
}
}
return 0;
}