BZOJ 1455 Rome game heap

Source: Internet
Author: User

BZOJ 1455 Rome game heap
Theme

The weight of n people is given. Each time the two teams are required to be combined into a heap, or the person with the smallest weight in a bunch of people is killed. Ask the number of people to be deleted each time.

Ideas

It's just a heap. It's gone. I chose the simplest random heap.

CODE
#include 
  
   #include 
   
    #include 
    
     #include #define MAX 1000010using namespace std;struct Heap{    Heap *son[2];    int val;    Heap(int _):val(_) {        son[0] = son[1] = NULL;    }    Heap() {}}*heap[MAX],mempool[MAX],*C = mempool + 1;Heap *Merge(Heap *x,Heap *y){    if(x == NULL)   return y;    if(y == NULL)   return x;    if(x->val > y->val)    swap(x,y);    bool k = rand()&1;    x->son[k] = Merge(x->son[k],y);    return x;}int points,asks;int src[MAX];bool killed[MAX];char s[10];int father[MAX];int Find(int x){    if(father[x] == x)  return x;    return father[x] = Find(father[x]);}int main(){    srand(19970806);    cin >> points;    for(int i = 1; i <= points; ++i)        father[i] = i;    for(int x,i = 1; i <= points; ++i) {        scanf("%d",&x);        heap[i] = new (C++)Heap(x);    }    cin >> asks;    for(int x,y,i = 1; i <= asks; ++i) {        scanf("%s",s);        if(s[0] == 'M') {            scanf("%d%d",&x,&y);            if(killed[x] || killed[y])  continue;            int fx = Find(x),fy = Find(y);            if(fx == fy)    continue;            father[fy] = fx;            heap[fx] = Merge(heap[fx],heap[fy]);        }        else {            scanf("%d",&x);            if(killed[x]) {                puts("0");                continue;            }            int fx = Find(x);            printf("%d\n",heap[fx]->val);            killed[heap[fx] - mempool] = true;            heap[fx] = Merge(heap[fx]->son[0],heap[fx]->son[1]);        }    }    return 0;}
    
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.