Cod sursa(job #1437172)

Utilizator maricasorinSorin-Gabriel maricasorin Data 17 mai 2015 01:14:58
Problema Paduri de multimi disjuncte Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include <iostream>
#include <fstream>

using namespace std;

int findset(int *t,int *h,int x)
{
    int i=x;
    while (t[x]!=0) x=t[x];
    if (i!=x)
    {
        t[i]=x;
 //       h[i]=1;
    }
    return x;
}

void uniune(int *t,int *h,int x,int y)
{
    if (h[x]<h[y])
    {
        t[x]=findset(t,h,y);
        h[x]++;
    }
        else
        {
            t[y]=findset(t,h,x);
            h[y]++;
        }
}

int main()
{
    ifstream f("disjoint.in");
    int a,b,c,*t,n,m,*h;
    f>>n>>m;
    t=new int [n+1];
    h=new int [n+1];
    for (int i=1;i<=n;i++) h[i]=t[i]=0;
    ofstream g("disjoint.out");
    for (int i=0;i<m;i++)
    {
        f>>a>>b>>c;
        //for (int i=1;i<=n;i++) cout<<findset(t,i)<<" ";
        //cout<<endl;
        if (a==1) uniune(t,h,b,c);
            else if (a==2)
            {
                if (findset(t,h,b)==findset(t,h,c)) g<<"DA\n";
                                else g<<"NU\n";
            }
    }
    return 0;
}