Pagini recente » Rating Laura M (laura2018) | Cod sursa (job #3253760)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m;
int t[100005];
int FindRoot(int x)
{
int y, rad = x;
while (t[rad] != 0)
rad = t[rad];
while (x != rad)
{
y = t[x];
t[x] = rad;
x = y;
}
return rad;
}
void Union(int x, int y)
{
t[y] = x;
}
int main()
{
int task, x, y;
fin >> n >> m;
while (m--)
{
fin >> task >> x >> y;
x = FindRoot(x); y = FindRoot(y);
if (task == 1) Union(x, y);
else fout << ((x == y) ? "DA\n" : "NU\n");
}
}