Cod sursa(job #1473128)

Utilizator lucian.okapiNestian Lucian-Dan lucian.okapi Data 18 august 2015 16:56:11
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <iostream>
#include <fstream>
#define NMAX 100020

using namespace std;

ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

int N,M;
int rang[NMAX],arbore[NMAX];

void Initiailizare()
{
    for(int i=1; i<=N; i++)
    {
        rang[i]=1;
        arbore[i]=i;
    }
}

int Find(int x)
{
    int rad , y;
    for(rad=x ; arbore[rad]!=rad ; rad=arbore[rad])
        ;
    while(arbore[x]!=x)
    {
        y=arbore[x];
        arbore[x]=rad;
        x=y;
    }
    return rad;
}

void Unire(int x, int y)
{
    int xRoot=Find(x);
    int yRoot=Find(y);
    if(xRoot == yRoot)
        return;
    if(rang[xRoot] < rang[yRoot])
        arbore[xRoot] = yRoot;
    else
    {
        arbore[yRoot] = xRoot;
        rang[xRoot] = rang[xRoot]+1;
    }
}

int main()
{
    int x , y , i , op;
    fin>>N>>M;
    Initiailizare();
    for(i = 1 ; i <= M; i++)
    {
        fin>>op>>x>>y;
        if(op == 2)
        {
            if(Find(x) == Find(y))
                fout<<"DA\n";
            else fout<<"NU\n";
        }
        if(op == 1)
        {
            if (Find(x) == Find(y))
                return 0;
            else Unire(Find(x),Find(y));
        }
    }

    cout<<"fuck";
    return 0;
}