Pagini recente » Cod sursa (job #2290520) | Cod sursa (job #3293199) | Cod sursa (job #1364819) | Cod sursa (job #3270919) | Cod sursa (job #716016)
Cod sursa(job #716016)
#include <fstream>
#include <cstdlib>
#define N_MAX 100011
using namespace std;
int f[N_MAX], r[N_MAX];
int father( int x )
{
int y, z;
for( y=x; y != f[y]; y=f[y] );
for( ; x != f[x]; x=z )
{
z=f[x];
f[x]=y;
}
return y;
}
inline void Unite( int x, int y )
{
if( r[x] == r[y] )
{
if( x != y )
f[x]=y;
++r[x];
}
else r[x]=r[y]=( r[x] <= r[y] ? r[x] : r[y] );
}
int main()
{
int N, M, i, x, y, op;
ifstream in( "disjoint.in" );
ofstream out( "disjoint.out" );
in>>N;
for( i=1; i <= N; ++i )
f[i]=i, r[i]=1;
for( in>>M; M; --M )
{
in>>op>>x>>y;
if( 2 == op )
out<<( father(x) == father(y) ? "DA" : "NU" )<<'\n';
else Unite( father(x), father(y) );
}
return EXIT_SUCCESS;
}