Cod sursa(job #376656)

Utilizator alexandru92alexandru alexandru92 Data 22 decembrie 2009 11:15:40
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
/* 
 * File:   main.cpp
 * Author: virtualdemon
 *
 * Created on December 22, 2009, 10:48 AM
 */
#include <vector>
#include <fstream>

/*
 * 
 */
using namespace std;
vector<unsigned int> 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 )
{
    father[x]=father[y]=min( x, y );

}
int main()
{unsigned int n, t, i, op, x, y, minim;
    ifstream in("disjoint.in");
    in>>n>>t;
    father.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;
}