Cod sursa(job #3041392)

Utilizator stefanvoicaVoica Stefan stefanvoica Data 31 martie 2023 13:47:19
Problema Heapuri cu reuniune Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include<bits/stdc++.h>
using namespace std;
ifstream fin ("mergeheap.in");
ofstream fout("mergeheap.out");
int n,m,head[102];
priority_queue<int>pq[102];

int main ()
{
    int op,x,y;
    fin>>x>>m;
    for (int i=1;i<=x;i++)
        head[i]=i;
    while (m--)
    {
        fin>>op>>x;
        x=head[x];
        if (op==2)
        {
            fout<<pq[x].top()<<'\n';
            pq[x].pop();
        }
        else
        {
            fin>>y;
            if (op==1)
                pq[x].push(y);
            else    { ///le torn in x
                y=head[y];
                if (pq[x].size() < pq[y].size())
                {
                    while (!pq[x].empty())
                    {
                        pq[y].push(pq[x].top());
                        pq[x].pop();
                    }
                    head[x]=y;
                }
                else {
                    while (!pq[y].empty())
                    {
                        pq[x].push(pq[y].top());
                        pq[y].pop();
                    }
                }
            }
        }
    }
}