Cod sursa(job #2004150)

Utilizator Johnny07Savu Ioan-Daniel Johnny07 Data 25 iulie 2017 01:26:19
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n,m,i,o,n1,n2,x,aux,a,y;

struct st
{
    int leg, adancime;
}arbore[100010];


int main()
{
f>>n>>m;
//cout<<arbore[5].leg;
for (i=1;i<=m;i++)
{
    f>>o>>n1>>n2;
    if (o==1)
    {
        x=n1;
        while (arbore[x].leg!=0) {x=arbore[x].leg;}
        aux=n1;
        while (arbore[aux].leg!=0) {a=aux;aux=arbore[aux].leg;arbore[a].leg=x;}
        arbore[x].leg=0;
        y=n2;
        while (arbore[y].leg!=0) {y=arbore[y].leg;}
        aux=n2;
        while (arbore[aux].leg!=0) {a=aux;aux=arbore[aux].leg;arbore[a].leg=y;}
        arbore[n2].leg=y;
        arbore[y].leg=0;

        if (arbore[x].adancime>arbore[y].adancime) arbore[y].leg=x;
        if (arbore[x].adancime<arbore[y].adancime) arbore[x].leg=y;
        if (arbore[x].adancime==arbore[y].adancime) {arbore[x].leg=y;arbore[y].adancime++;}
    }
    if (o==2)
    {
        x=n1;
        while (arbore[x].leg!=0) {x=arbore[x].leg;}
        aux=n1;
        while (arbore[aux].leg!=0) {a=aux;aux=arbore[aux].leg;arbore[a].leg=x;}
        arbore[x].leg=0;
        y=n2;
        while (arbore[y].leg!=0) {y=arbore[y].leg;}
        aux=n2;
        while (arbore[aux].leg!=0) {a=aux;aux=arbore[aux].leg;arbore[a].leg=y;}
        arbore[n2].leg=y;
        arbore[y].leg=0;
        if (y==x) g<<"DA\n";
        else g<<"NU\n";
    }



}
//cout<<arbore[2].leg;





    return 0;
}