Pagini recente » Cod sursa (job #592334) | Cod sursa (job #1249094) | Cod sursa (job #2268476) | Cod sursa (job #193933) | Cod sursa (job #3317610)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int sef[100001];
int boss ( int x )
{
if ( sef [x] == x )
return x;
else return sef[x] = boss ( sef[x] );
}
void unire ( int x, int y )
{
sef[boss(y)] = boss(x);
}
int main()
{
int n, m;
f >> n >> m;
for ( int i = 1; i <= n; i ++ )
sef[i] = i;
for ( int i = 1; i <= m; i ++ )
{
int c, x, y;
f >> c >> x >> y;
if ( c == 1 )
unire ( x, y );
else
{
if ( boss(x) == boss (y) )
g << "DA" << '\n';
else g << "NU" << '\n';
}
}
return 0;
}