Pagini recente » Cod sursa (job #3151430) | Cod sursa (job #2957728) | Rating SmileSmile (icansmile) | Cod sursa (job #1820617) | Cod sursa (job #376659)
Cod sursa(job #376659)
/*
* File: main.cpp
* Author: virtualdemon
*
* Created on December 22, 2009, 10:48 AM
*/
#include <vector>
#include <fstream>
/*
*
*/
using namespace std;
vector<unsigned int> rank, father;
inline unsigned int find( unsigned int x )
{
if( father[x] == x )
return x;
return find( father[x] );
}
inline void unification( unsigned int x, unsigned int y )
{
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, i, op, x, y, minim;
ifstream in("disjoint.in");
in>>n>>t;
father.resize(n);
rank.resize(n);
for( i=0; i < n; ++i )
father[i]=i;
ofstream out("disjoint.out");
for( i=0; i < t; ++i )
{
in>>op>>x>>y;
x-=1;
y-=1;
if( 1 == op )
{
unification( find(x), find(y) );
continue;
}
if( find(x) == find(y) )
out<<"DA\n";
else out<<"NU\n";
}
return 0;
}