Cod sursa(job #1211474)

Utilizator Mihai_ChihaiMihai Chihai Mihai_Chihai Data 22 iulie 2014 17:43:07
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <fstream>
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int p[100010],n,m,i,px,py,x,y,op,xx,yy;

int radacina(int x)
{
    while (p[x]!=x) x=p[x];
    return x;
}

int main()
{
    cin>>n>>m;
    for (int i=1;i<=n;i++) p[i]=i;
    while (m--)
    {
        cin>>op>>x>>y;
        if (op==1)
        {
            px=radacina(x);
            py=radacina(y);
            p[px]=py;
        }  else
        {
            px=radacina(x);
            py=radacina(y);
            if (px==py) cout<<"DA\n"; else cout<<"NU\n";
            while (p[x]!=px) xx=x, x=p[x], p[xx]=px;
            while (p[y]!=py) yy=y, y=p[y], p[yy]=py;
        }
    }
    return 0;
}