zoukankan      html  css  js  c++  java
  • [BZOJ 1455]罗马游戏(左偏树+并查集)

    Description

    罗马皇帝很喜欢玩杀人游戏。 他的军队里面有n个人,每个人都是一个独立的团。最近举行了一次平面几何测试,每个人都得到了一个分数。 皇帝很喜欢平面几何,他对那些得分很低的人嗤之以鼻。他决定玩这样一个游戏。 它可以发两种命令: 1. Merger(i, j)。把i所在的团和j所在的团合并成一个团。如果i, j有一个人是死人,那么就忽略该命令。 2. Kill(i)。把i所在的团里面得分最低的人杀死。如果i这个人已经死了,这条命令就忽略。 皇帝希望他每发布一条kill命令,下面的将军就把被杀的人的分数报上来。(如果这条命令被忽略,那么就报0分)

    Solution

    比较裸的一道左偏树

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<cstring>
    #define MAXN 1000005
    using namespace std;
    int n,m,father[MAXN];
    bool die[MAXN];
    struct Node
    {
        int lch,rch,w,d;
        Node():lch(0),rch(0),w(0),d(0){}
    }heap[MAXN];
    int merge(int x,int y)
    {
        if(!x||!y)return x+y;
        if(heap[x].w>heap[y].w)swap(x,y);
        heap[x].rch=merge(y,heap[x].rch);
        if(heap[heap[x].lch].d<heap[heap[x].rch].d)swap(heap[x].lch,heap[x].rch);
        heap[x].d=heap[heap[x].rch].d+1;
        return x;
    }
    int find(int x)
    {
        if(x==father[x])return x;
        return father[x]=find(father[x]);
    }
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&heap[i].w);
            father[i]=i;}
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            char opt[3];int x,y;
            scanf("%s",opt);
            if(opt[0]=='M')
            {
                scanf("%d%d",&x,&y);
                if(die[x]||die[y])continue;
                int fx=find(x),fy=find(y);
                if(fx!=fy)
                father[fx]=father[fy]=merge(fx,fy);
            }
            else
            {
                scanf("%d",&x);
                if(die[x])printf("0
    ");
                else
                {
                    int fx=find(x);
                    die[fx]=1;
                    printf("%d
    ",heap[fx].w);
                    father[fx]=merge(heap[fx].lch,heap[fx].rch);
                    father[father[fx]]=father[fx];
                }
            }
        }
        return 0;
    } 
  • 相关阅读:
    hdu 1455 N个短木棒 拼成长度相等的几根长木棒 (DFS)
    hdu 1181 以b开头m结尾的咒语 (DFS)
    hdu 1258 从n个数中找和为t的组合 (DFS)
    hdu 4707 仓鼠 记录深度 (BFS)
    LightOJ 1140 How Many Zeroes? (数位DP)
    HDU 3709 Balanced Number (数位DP)
    HDU 3652 B-number (数位DP)
    HDU 5900 QSC and Master (区间DP)
    HDU 5901 Count primes (模板题)
    CodeForces 712C Memory and De-Evolution (贪心+暴力)
  • 原文地址:https://www.cnblogs.com/Zars19/p/6901587.html
Copyright © 2011-2022 走看看