Pagini recente » Cod sursa (job #2965043) | Cod sursa (job #1021851) | Cod sursa (job #2693991) | Cod sursa (job #2476814) | Cod sursa (job #376679)
Cod sursa(job #376679)
/*
* File: main.cpp
* Author: virtualdemon
*
* Created on December 22, 2009, 11:29 AM
*/
#include <vector>
#include <fstream>
/*
*
*/
using namespace std;
vector<unsigned int> rank, father;
inline unsigned int find( unsigned int x ) //path compresion
{unsigned int y, z;
for( y=x; father[y] != y; y=father[y] );
for( ; father[x] != x; )
{
z=father[x];
father[x]=y;
x=z;
}
return y;
}
inline void Union( int x, int y ) //rank union
{
if( rank[x] == rank[y] )
{
if( x != y )
father[y]=x;
++rank[y];
}
else rank[x]=rank[y]=min( rank[x], rank[y] );;
}
int main()
{unsigned int n, t, x, y, op, i;
ifstream in("disjoint.in");
in>>n>>t;
father.resize(n);
rank.resize(n);
for( i=0; i < n; father[i]=i, ++i );
ofstream out("disjoint.out");
for( i=0; i < t; ++i )
{
in>>op>>x>>y;
x-=1;
y-=1;
if( 1 == op )
{
Union( find(x), find(y) );
continue;
}
if( find(x) == find(y) )
out<<"DA\n";
else out<<"NU\n";
}
return 0;
}