Pagini recente » Cod sursa (job #2788266) | Cod sursa (job #2314278) | Cod sursa (job #884760) | Cod sursa (job #1041493) | Cod sursa (job #437255)
Cod sursa(job #437255)
/*
* File: main.cpp
* Author: VirtualDemon
*
* Created on April 9, 2010, 3:28 PM
*/
#include <vector>
#include <cstdlib>
#include <fstream>
/*
*
*/
using namespace std;
vector< int > f, r;
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] );
}
int main(int argc, char** argv)
{
int N, M, i, j;
ifstream in( "disjoint.in" );
ofstream out( "disjoint.out" );
in>>N>>M;
for( i=0; i < N; ++i )
{
f.push_back(i);
r.push_back(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;
}