Pagini recente » Cod sursa (job #2823537) | Cod sursa (job #1632533) | Cod sursa (job #1966141) | Cod sursa (job #1451262) | Cod sursa (job #437257)
Cod sursa(job #437257)
/*
* File: main.cpp
* Author: VirtualDemon
*
* Created on April 9, 2010, 3:28 PM
*/
#include <cstdlib>
#include <fstream>
#define Nmax 100001
/*
*
*/
using namespace std;
int f[Nmax], r[Nmax];
inline int find( int x )
{
int y, z;
for( y=f[x]; y != f[y]; y=f[y] );
for( ; x != f[x]; )
{
z=f[x];
f[x]=y;
x=z;
}
return y;
}
inline const int& min( const int& x, const int& y )
{
if( x < y )
return x;
return y;
}
inline void unite( const int& x, const int& y )
{/*
if( r[x] == r[y] )
{
if( x != y )
f[y]=x;
++r[y];
}
else r[x]=r[y]=min( r[x], r[y] );*/
f[x]=f[y];
}
int main(int argc, char** argv)
{
int N, M, i, j;
ifstream in( "disjoint.in" );
ofstream out( "disjoint.out" );
in>>N>>M;
for( i=1; i <= N; ++i )
{
f[i]=i;
r[i]=1;
}
for( ; M; --M )
{
in>>i;
if( 1 == i )
{
in>>i>>j;
unite( find(i), find(j) );
}
else {
in>>i>>j;
if( find(i) == find(j) )
out<<"DA\n";
else out<<"NU\n";
}
}
return EXIT_SUCCESS;
}