Cod sursa(job #385230)

Utilizator alexandru92alexandru alexandru92 Data 22 ianuarie 2010 13:15:17
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
/* 
 * 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;
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) );
}
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);
    for( i=0; i < m; ++i )
    {
        in>>op>>x>>y;
        x=find( x-1 );
        y=find( y-1 );
        switch( op )
        {
            case 1 : father[x]=y; break;
            case 2 : if(  x  ==  y  )
                       out<<"DA\n";
                     else out<<"NU\n";
        }
    }
    return 0;
}