Cod sursa(job #2776259)

Utilizator BlaugranasEnal Gemaledin Blaugranas Data 19 septembrie 2021 09:48:28
Problema Heapuri cu reuniune Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include<fstream>
using namespace std;
ifstream F("mergeheap.in");
ofstream G("mergeheap.out");
typedef struct O {
    int v;
    struct O *t,*i,*r;
}N;
N *W(int v)
{
    N *t=(N*)malloc(sizeof(N));
    t->i=t->r=NULL;
    t->v=v,t->r=t->i=NULL;
    return t;
}
N *M(N *a,N *b)
{
    if(!a)
        return b;
    if(!b)
        return a;
    N *z;
    if(b->v>a->v)
        z=a,a=b,b=z;
    if(!a->i) {
        a->i=b;
        return a;
    } else {
        b->r=a->i,a->i=b;
        return a;
    }
    return NULL;
}
N *I(int v,N *h)
{
    N *t=W(v);
    h=M(t,h);
    return h;
}
N *S(N *h)
{
    if(!h||!h->r)
        return h;
    else {
        N *c,*d,*e;
        c=h,d=h->r,e=h->r->r,c->r=d->r=NULL;
        return M(M(c,d),S(e));
    }
    return NULL;
}
N *D(N *h)
{
    G<<h->v<<"\n";
    return S(h->i);
}
int n,m,o,a,b;
N *u[105];
int main()
{
    F>>n>>m;
    while(m--) {
        F>>o;
        if(o==1)
            F>>a>>b,u[a]=I(b,u[a]);
        else if(o==2)
            F>>a,u[a]=D(u[a]);
        else
            F>>a>>b,u[a]=M(u[a],u[b]),u[b]=NULL;
    }
    return 0;
}