Pagini recente » Cod sursa (job #2530937) | Cod sursa (job #1799422) | Cod sursa (job #437467) | Cod sursa (job #1579682) | Cod sursa (job #2710537)
#include <fstream>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
int n, t[100001];
int parent ( int x ) {
while ( t[x] > 0 )
x = t[x];
return x;
}
bool OK ( int x, int y ) {
if ( parent(x) == parent(y) )
return true;
return false;
}
void Union ( int x, int y ) {
int tx = parent(x);
int ty = parent(y);
if ( ty > tx )
swap ( tx, ty );
t[ tx ] += t[ ty ];
t[ ty ] = tx;
}
int main()
{
int m, cod, x, y;
fin >> n >> m;
for ( int i = 1; i <= n; i++ )
t[ i ] = -1;
while ( m-- ) {
fin >> cod >> x >> y;
if ( cod == 1 )
Union( x, y );
else {
if ( OK(x, y) )
fout << "DA\n";
else
fout << "NU\n";
}
}
return 0;
}