Cod sursa(job #2532721)

Utilizator AlexTacuTacu Alexandru AlexTacu Data 28 ianuarie 2020 10:19:36
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <bits/stdc++.h>

using namespace std;

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

struct art
{
    int x,h;
};

art v[100005];
int n,m;
int a,b,c;
int r;

void compresie(int a)
{
    if(v[a].h!=1)
        compresie(v[a].x);
    else
    {
        r=a;
        return ;
    }
    v[a].x=r;
    v[a].h=2;
}

int main()
{
    in>>n>>m;
    for(int i=1; i<=n; i++)
    {
        v[i].x=i;
        v[i].h=1;
    }
    while(m)
    {
        in>>a>>b>>c;
        if(a==1)
        {
            if(v[b].h<=v[c].h)
            {
                v[v[b].x].x=c;
                if(v[b].h!=1)
                    v[v[b].x].h+=v[c].h;
                v[b].h+=v[c].h;
            }
            else
            {
                 v[v[c].x].x=b;
                if(v[c].h!=1)
                    v[v[c].x].h+=v[b].h;
                v[c].h+=v[b].h;
            }
        }
        else
        {
            int z=b,zz=c;
            compresie(b);
            z=r;
            compresie(c);
            zz=r;
            if(z==zz)
                out<<"DA \n";
            else
                out<<"NU \n";
        }
        m--;
    }
    return 0;
}