Pagini recente » Cod sursa (job #207205) | Cod sursa (job #2387174) | Cod sursa (job #1990920) | Cod sursa (job #677285) | Cod sursa (job #2366092)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
int n, m;
int a[100007];
/// a[i] - nodul tata pentru nodul i
void Union(int x, int y)
{
a[x] = y;
}
int Find(int x)
{
int rad, y;
rad = x;
/// cautam nodul tata pentru nodul x
while (a[rad] != 0)
rad = a[rad];
/// conectam toti tatii nodului x (fiii nodului rad) la rad
while (x != rad)
{
y = a[x];
a[x] = rad;
x = y;
}
return rad;
}
int main()
{
int op, x, y;
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
fin >> op >> x >> y;
if (op == 1)
{
x = Find(x);
y = Find(y);
/// conectam tatii intre ei
Union(x, y);
}
else
{
x = Find(x);
y = Find(y);
if (x == y)
fout << "DA\n";
else fout << "NU\n";
}
}
fin.close();
fout.close();
return 0;
}