Pagini recente » Cod sursa (job #296450) | Cod sursa (job #2369080) | Cod sursa (job #1963926) | Cod sursa (job #1972630) | Cod sursa (job #1621747)
#include <fstream>
#define Nmax 100001
using namespace std;
ifstream f ("disjoint.in");
ofstream g ("disjoint.out");
int n, m, t[ Nmax ], h[ Nmax ];
void init ()
{
for ( int i = 1; i <= n; ++ i )
{
t[ i ] = i;
h[ i ] = 1;
}
}
int multime ( int x )
{
if ( t[ x ] == x )
return x;
t[ x ] = multime( t[ x ] );
return t[ x ];
}
void unificare ( int x, int y )
{
x = multime ( x );
y = multime ( y );
if ( h[ y ] > h[ x ] )
t[ x ] = y;
else
t[ y ] = x;
if ( h[ x ] == h[ y ] )
h[ x ] ++;
}
void run ()
{
int x, y, op;
f >> n >> m;
init ();
for ( int i = 1; i <= m; ++ i )
{
f >> op >> x >> y;
if ( op == 1 )
unificare ( x, y );
else
{
if ( multime( x ) == multime ( y ) )
g << "DA" << endl;
else
g << "NU" << endl;
}
}
}
int main ()
{
run();
return 0;
}