Pagini recente » Cod sursa (job #3236912) | Cod sursa (job #2747727) | Cod sursa (job #2413250) | Cod sursa (job #2565723) | Cod sursa (job #385228)
Cod sursa(job #385228)
/*
* File: main.cpp
* Author: virtualdemon
*
* Created on January 22, 2010, 1:02 PM
*/
#include <vector>
#include <fstream>
/*
*
*/
using namespace std;
typedef unsigned int u;
vector<u> father, rank;
u find( u x )
{
u y, z;
for( y=x; y != father[y]; y=father[y] );
for( ; x != father[x]; )
{
z=father[x];
father[x]=y;
x=z;
}
return y;
}
inline u min( u x, u y )
{
return y^( (x^y) & -(x<y) );
}
inline void Unite( u x, u y )
{
if( rank[x] == rank[y] )
{
if( x != y )
father[y]=x;
++rank[y];
return;
}
rank[x]=rank[y]=min( rank[x], rank[y] );
}
int main()
{u n, m, i, op, x, y;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
in>>n>>m;
for( i=0; i < n; ++i )
father.push_back(i), rank.push_back(1);
for( i=0; i < m; ++i )
{
in>>op>>x>>y;
x-=1;
y-=1;
switch( op )
{
case 1 : Unite( find( x ), find( y ) ); break;
case 2 : if( find( x ) == find( y ) )
out<<"DA\n";
else out<<"NU\n";
}
}
return 0;
}