Cod sursa(job #2027913)

Utilizator amaliarebAmalia Rebegea amaliareb Data 26 septembrie 2017 21:11:53
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#define mod 30011
//i'm never leaving this project

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

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==2)
        {
            while(father[x]) x=father[x];
            while(father[y]) y=father[y];
            if(x==y) g<<"DA\n";
            else g<<"NU\n";
        }
        else
        {
            X=x; Y=y;
            while(father[X]) X=father[X];
            while(father[Y]) Y=father[Y];
            if(rang[X]>=rang[Y])
            {
                father[Y]=X;
                rang[X]=max(rang[Y]+1,rang[X]);
                while(father[x])
                {
                    xx=father[x];
                    father[x]=X;
                    x=xx;
                }
            }
            else
            {
                father[X]=Y;
                rang[Y]=max(rang[X]+1,rang[Y]);
                while(father[y])
                {
                    yy=father[y];
                    father[y]=Y;
                    y=yy;
                }
            }
        }
    }
    return 0;
}