Cod sursa(job #1994454)

Utilizator amaliarebAmalia Rebegea amaliareb Data 24 iunie 2017 23:44:36
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <iostream>
#include <fstream>
#include <cstring>
#define mod 666013

using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n,i,j,v[100005],father[100005],rang[100005],tip,m,f1,f2,x,y,aux;

int main()
{
    f>>n>>m;
    for(i=1;i<=n;i++) rang[i]=1;
    for(i=1;i<=m;i++)
    {
        f>>tip>>x>>y;
        if(tip==1)
        {
            f1=x;
            f2=y;
            while(father[f1]!=0) f1=father[f1];
            while(father[f2]!=0) f2=father[f2];
            if(rang[f1]>=rang[f2]) father[f2]=f1;
            else father[f1]=f2;
        }
        else
        {
            f1=x;
            f2=y;
            while(father[f1]!=0) f1=father[f1];
            while(father[f2]!=0) f2=father[f2];
            if(f1==f2) g<<"DA\n";
            else g<<"NU\n";
            while(x!=f1 && father[x]!=f1)
            {
                aux=father[x];
                father[x]=f1;
                x=aux;
            }
            while(y!=f2 && father[y]!=f2)
            {
                aux=father[y];
                father[y]=f2;
                y=aux;
            }
        }
    }
    return 0;
}