Cod sursa(job #3297580)

Utilizator Arhiva_EducationalaArhiva Educationala Arhiva_Educationala Data 22 mai 2025 22:33:44
Problema Heapuri cu reuniune Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.87 kb
#include <bits/stdc++.h>

using namespace std;

const int NMAX = 100;

priority_queue <int> pq[NMAX + 1];

int main() {
    ifstream fin( "mergeheap.in" );
    ofstream fout( "mergeheap.out" );
    int n, m;
    fin >> n >> m;
    for ( int i = 1, tip; i <= m; i ++ ) {
        fin >> tip;
        if ( tip == 1 ) {
            int m, x;
            fin >> m >> x;
            pq[m].push( x );
        } else if ( tip == 2 ) {
            int m;
            fin >> m;
            fout << pq[m].top() << '\n';
            pq[m].pop();
        } else {
            int a, b;
            fin >> a >> b;
            if ( pq[b].size() > pq[a].size() ) {
                swap( pq[a], pq[b] );
            }
            while ( !pq[b].empty() ) {
                pq[a].push( pq[b].top() );
                pq[b].pop();
            }
        }
    }
    return 0;
}