Pagini recente » Clasament lot2010mixt | Cod sursa (job #2081093) | Cod sursa (job #2629245) | Cod sursa (job #79107) | Cod sursa (job #3126605)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("mergeheap.in");
ofstream fout("mergeheap.out");
/*class PairingHeap{
class Node{
public:
int data;
Node* child;
Node* next;
Node(int data){
this->data = data;
child = nullptr;
next = nullptr;
}
};
Node* root;
public:
PairingHeap(){
root = nullptr;
}
void insert(int data){
Node* node = new Node(data);
if(root == nullptr){
root = node;
}else{
root = merge(root, node);
}
}
Node* merge(Node* node1, Node* node2){
if(node1 == nullptr){
return node2;
}
if(node2 == nullptr){
return node1;
}
if(node1->data < node2->data){
Node* temp = node1;
node1 = node2;
node2 = temp;
}
Node* child = node1->child;
if (child == nullptr) {
node1->child = node2;
} else {
while (child->next != nullptr) {
child = child->next;
}
child->next = node2;
}
return node1;
}
int getMax(){
return root->data;
}
void extractMax(){
Node* current = root->child;
root = nullptr;
while(current!=nullptr)
{
Node* next = current->next;
current->next = nullptr;
root = merge(root,current);
current = next;
}
}
PairingHeap merge(PairingHeap& heap1, PairingHeap& heap2){
PairingHeap heap;
heap.root = merge(heap1.root, heap2.root);
return heap;
}
void Join(PairingHeap& heap){
root = merge(root, heap.root);
}
};
*/
struct HeapNode {
int key;
HeapNode *leftChild;
HeapNode *nextSibling;
HeapNode():
leftChild(NULL), nextSibling(NULL) {}
// creates a new node
HeapNode(int key_, HeapNode *leftChild_, HeapNode *nextSibling_):
key(key_), leftChild(leftChild_), nextSibling(nextSibling_) {}
// Adds a child and sibling to the node
void addChild(HeapNode *node) {
if(leftChild == NULL)
leftChild = node;
else {
node->nextSibling = leftChild;
leftChild = node;
}
}
};
// Returns true if root of the tree
// is null otherwise returns false
bool Empty(HeapNode *node) {
return (node == NULL);
}
// Function to merge two heaps
HeapNode *Merge(HeapNode *A, HeapNode *B)
{
// If any of the two-nodes is null
// the return the not null node
if(A == NULL) return B;
if(B == NULL) return A;
// To maintain the min heap condition compare
// the nodes and node with minimum value become
// parent of the other node
if(A->key < B->key) {
A->addChild(B);
return A;
}
else {
B->addChild(A);
return B;
}
return NULL; // Unreachable
}
// Returns the root value of the heap
int Top(HeapNode *node) {
return node->key;
}
// Function to insert the new node in the heap
HeapNode *Insert(HeapNode *node, int key) {
return Merge(node, new HeapNode(key, NULL, NULL));
}
// This method is used when we want to delete root node
HeapNode *TwoPassMerge(HeapNode *node) {
if(node == NULL || node->nextSibling == NULL)
return node;
else {
HeapNode *A, *B, *newNode;
A = node;
B = node->nextSibling;
newNode = node->nextSibling->nextSibling;
A->nextSibling = NULL;
B->nextSibling = NULL;
return Merge(Merge(A, B), TwoPassMerge(newNode));
}
return NULL; // Unreachable
}
// Function to delete the root node in heap
HeapNode *Delete(HeapNode *node) {
return TwoPassMerge(node->leftChild);
}
struct PairingHeap {
HeapNode *root;
PairingHeap():
root(NULL) {}
bool Empty(void) {
return ::Empty(root);
}
int Top(void) {
return ::Top(root);
}
void Insert(int key) {
root = ::Insert(root, key);
}
void Delete(void) {
root = ::Delete(root);
}
void Join(PairingHeap other) {
root = ::Merge(root, other.root);
}
};
int main() {
int n, q;
fin>>n;
PairingHeap heap[101];
fin>>q;
for(int i=0;i<q;i++){
int a,b,c;
fin>>a;
if(a==1){
fin>>b>>c;
heap[b].Insert(c);
}
else if(a==2){
fin>>b;
fout<<heap[b].Top()<<endl;
heap[b].Delete();
}
else if(a==3){
fin>>b>>c;
heap[b].Join(heap[c]);
}
}
return 0;
}