Cod sursa(job #1235410)

Utilizator httpsLup Vasile https Data 29 septembrie 2014 19:14:15
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.76 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream f("disjoint.in");
ofstream g("disjoint.out");

#define cout g
#define nmax 100010

int n,m,i,x,y,tip;
int R[nmax],T[nmax];

int rad(int x)
{
    int y,r=x;

    while (r!=T[r]) r=T[r];

  /*  for(;T[x]!=x;)
    {
        y=T[x];
        T[x]=r;
        x=y;
    }*/
    return r;
}

void uneste(int a,int b)
{
    if (R[a]>=R[b]) T[b]=a;
    else T[a]=b;

    if(R[a]==R[b]) ++R[a];
}

int main()
{
    f>>n>>m;
    for(i=1;i<=n;++i) T[i]=i,R[i]=1;
    for(;m;--m)
    {
        f>>tip>>x>>y;
        if(tip==1) uneste(rad(x),rad(y));
        else if(rad(x)==rad(y)) cout<<"DA\n";
                                else cout<<"NU\n";
    }

    return 0;
}