Cod sursa(job #2532709)

Utilizator AlexTacuTacu Alexandru AlexTacu Data 28 ianuarie 2020 09:54:14
Problema Paduri de multimi disjuncte Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 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[b].x=c;
                v[b].h+=v[c].h;
            }
            else
            {
                v[c].x=b;
                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;
    }