左偏树 板子

来源:互联网 发布:php网络爬虫软件 编辑:程序博客网 时间:2024/05/16 01:54

大神说左偏树很有用,就学了下,贴个板子


例题:BZOJ1455 罗马游戏


code:

#include<set>#include<map>#include<deque>#include<queue>#include<stack>#include<cmath>#include<ctime>#include<vector>#include<string>#include<bitset>#include<cstdio>#include<cstdlib>#include<cstring>#include<climits>#include<complex>#include<iostream>#include<algorithm>#define LL long longusing namespace std; const int maxn = 1100000;struct node{    int dist,c,lc,rc;}tree[maxn];int fa[maxn];bool dead[maxn];int n,m; int find_( int x ){    if( fa[x] == x ) return x;    return fa[x] = find_( fa[x] );}int merge( int x,int y ){    if( !x || !y ) return x|y;    if( tree[x].c > tree[y].c ) swap( x,y );    tree[x].rc = merge( tree[x].rc,y );    fa[tree[x].rc] = x;    tree[x].dist = tree[tree[x].rc].dist+1;    if( tree[tree[x].lc].dist < tree[tree[x].rc].dist )        swap( tree[x].lc,tree[x].rc );    return x;} int main(){    char st[110];    int x,y;         scanf("%d",&n);    for( int i=1;i<=n;i++ )    {        scanf("%d",&tree[i].c);        dead[i] = false;        fa[i] = i;        tree[i].dist = 0;        tree[i].lc = tree[i].rc = 0;    }    scanf("%d",&m);    while( m-- )    {        scanf("%s",st);        if( st[0] == 'M' )        {            scanf("%d%d",&x,&y);            if( !dead[x] && !dead[y] )            {                int f1 = find_( x ), f2 = find_( y );                if( f1 != f2 )  merge( f1,f2 );            }        }        else        {            scanf("%d",&x);            if( !dead[x] )            {                int f1 = find_( x );                printf("%d\n",tree[f1].c);                dead[f1] = true;                x = merge( tree[f1].lc,tree[f1].rc );                fa[x] = x; fa[f1] = x;            }            else printf("0\n");        }    }         return 0;}


0 0