Cod sursa(job #2917885)

Utilizator andrei_C1Andrei Chertes andrei_C1 Data 8 august 2022 14:36:56
Problema Heapuri cu reuniune Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
// solutie ok, cu small-to-large folosind priority_queue (va lua aproximativ 100p)
#include <bits/stdc++.h>

using namespace std;

ifstream fin("mergeheap.in");
ofstream fout("mergeheap.out");

const int NMAX = 100;

int n, q;
priority_queue<int> pq[NMAX + 1];

int main() {
	fin >> n >> q;

	for(int i = 1; i <= q; i++) {
		int task;
		fin >> task;

		if(task == 1) {
			int m, x;
			fin >> m >> x;

			pq[m].push(x);
		} else if(task == 2) {
			int m;
			fin >> m;

			fout << pq[m].top() << '\n';
			pq[m].pop();
		} else {
			int a, b;
			fin >> a >> b;

			if(pq[a].size() < pq[b].size()) {
				pq[a].swap(pq[b]);
			}

			while(!pq[b].empty()) {
				pq[a].push(pq[b].top());
				pq[b].pop();
			}
		}
	}
	return 0;
}