BZOJ 1455 Rome game left Tree, bzoj1455

Source: Internet
Author: User

BZOJ 1455 Rome game left Tree, bzoj1455

Given n vertices, each vertex has a weight and provides two operations:

1. Merge the set of two vertices

2. Delete the smallest vertex in the set where a vertex is located and output the weight value.

Very bare and heap n <= 100 W heuristic merge. No need to think about it.

The left-side tree is fast ~

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define M 1001001using namespace std;struct abcd{    abcd *ls,*rs;    int h,pos,score;    abcd(int x,int y);}*null=new abcd(0,0x3f3f3f3f),*tree[M];abcd :: abcd(int x,int y){    ls=rs=null;    if(!null) h=-1;    else h=0;    pos=x;score=y;}abcd* Merge(abcd *x,abcd *y){    if(x==null) return y;    if(y==null) return x;    if(x->score>y->score)        swap(x,y);    x->rs=Merge(x->rs,y);    if(x->ls->h<x->rs->h)        swap(x->ls,x->rs);    x->h=x->rs->h+1;    return x;}int n,m;int fa[M];bool dead[M];int Find(int x){    if(!fa[x]||fa[x]==x)        return fa[x]=x;    return fa[x]=Find(fa[x]);}void Unite(int x,int y){    x=Find(x);y=Find(y);    if(x==y) return ;    fa[x]=y;    tree[y]=Merge(tree[x],tree[y]);}int main(){     //freopen("1455.in","r",stdin);    //freopen("1455.out","w",stdout);     int i,x,y;    char p[10];    cin>>n;    for(i=1;i<=n;i++)        scanf("%d",&x),tree[i]=new abcd(i,x);    cin>>m;    for(i=1;i<=m;i++)    {        scanf("%s",p);        if(p[0]=='M')        {            scanf("%d%d",&x,&y);            if(dead[x]||dead[y])                continue;            Unite(x,y);        }        else        {            scanf("%d",&x);            if(dead[x])            {                puts("0");                continue;            }            x=Find(x);            printf("%d\n",tree[x]->score);            dead[tree[x]->pos]=1;            tree[x]=Merge(tree[x]->ls,tree[x]->rs);        }    }}



Q: What books or textbooks are used to explain advanced data structures? For details, it is best to include AVL, RMQ, and left-side tree. The more comprehensive, the better.

Liu rujia's competition in algorithms and informatics.
 
Q: Can I calculate the height of the left-side tree? How can this problem be achieved?

What is the height required for the left-side tree? It seems that it is of little practical significance.
If you really want to traverse the entire tree, you can find it, but the efficiency is very low.

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.