Pagini recente » Monitorul de evaluare | Monitorul de evaluare | Cod sursa (job #2391721) | Monitorul de evaluare | Cod sursa (job #2728676)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m, cod, x, y;
int sef[100001];
int gaseste(int a)
{
if(sef[a] == a)
{
return a;
}
sef[a] = gaseste(sef[a]);
return sef[a];
}
void uneste(int a, int b)
{
sef[gaseste(a)] = gaseste(b);
}
int main()
{
fin >> n >> m;
for(int i = 1; i <= n; ++i)
{
sef[i] = i;
}
for(int i = 1; i <= m; ++i)
{
fin >> cod >> x >> y;
if(cod == 1)
{
uneste(x, y);
}
else
{
if(gaseste(x) == gaseste(y))
{
fout << "DA";
}
else fout << "NU";
fout << '\n';
}
}
return 0;
}