zoukankan      html  css  js  c++  java
  • HDU1512 Monkey King

    题面

    Problem Description

    Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its own way and none of them knows each other. But monkeys can't avoid quarrelling, and it only happens between two monkeys who does not know each other. And when it happens, both the two monkeys will invite the strongest friend of them, and duel. Of course, after the duel, the two monkeys and all of there friends knows each other, and the quarrel above will no longer happens between these monkeys even if they have ever conflicted.
    Assume that every money has a strongness value, which will be reduced to only half of the original after a duel(that is, 10 will be reduced to 5 and 5 will be reduced to 2).
    And we also assume that every monkey knows himself. That is, when he is the strongest one in all of his friends, he himself will go to duel.

    Input

    There are several test cases, and each case consists of two parts.
    First part: The first line contains an integer N(N<=100,000), which indicates the number of monkeys. And then N lines follows. There is one number on each line, indicating the strongness value of ith monkey(<=32768).
    Second part: The first line contains an integer M(M<=100,000), which indicates there are M conflicts happened. And then M lines follows, each line of which contains two integers x and y, indicating that there is a conflict between the Xth monkey and Yth.

    Output

    For each of the conflict, output -1 if the two monkeys know each other, otherwise output the strongness value of the strongest monkey in all friends of them after the duel.

    Sample Input

    5
    20
    16
    10
    10
    4
    5
    2 3
    3 4
    3 5
    4 5
    1 5
    

    Sample Output

    8
    5
    5
    -1
    10
    

    题目大意

    给定一些点, 开始时每个点都是独立的, 并且有点权; 对于每一次操作, 给定两个点(x)(y), 要求:

    • 假如这两个点在同一集合内, 则输出(-1)
    • 假如这两个点不在同一个集合内, 则分别在两个点所在的集合中找到一个权值最大的点, 把它的权值减半, 然后把这两个点所在的集合合并到一起, 输出此时这个集合中的最大点权.

    Solution

    左偏树的模板题.
    注意是如何在左偏树上找到根节点的: 用并査集来维护, 每次merge时修改路径(实际上就是两棵左偏树的最右链)上节点的父亲节点就可以了.
    时间复杂度证明: 我们发现, 对于一棵点数为(n)的左偏树, 其最右链的长度不超过(log n), 因此合并两棵大小为(n)的左偏树的时间复杂度为(O(log n)). 我们考虑开始时每棵左偏树都只有一个节点, 则从初始状态合并至所有节点都在一棵树上的时间复杂度为$$Oleft(frac{n}{2^1} log 2^0 + frac{n}{2^2} log 2^1 + frac{n}{2^3} log 2^2 + ... + frac{n}{2^{k + 1}} log 2^k ight) = Oleft(frac{n}{2} log n ight) = O(n log n)$$

    #include <cstdio>
    #include <cctype>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    
    const int N = (int)1e5;
    
    namespace Zeonfai
    {
        inline int getInt()
        {
            int a = 0, sgn = 1;
            char c;
    
            while(! isdigit(c = getchar()))
                if(c == '-')
                    sgn *= -1;
                else if(c == EOF)
                    exit(0);
    
            while(isdigit(c))
                a = a * 10 + c - '0', c = getchar();
    
            return a * sgn;
        }
    
        void _print(int a)
        {
            if(! a)
                return;
    
            _print(a / 10);
            putchar('0' + a % 10);
        }
    
        inline void println(int a)
        {
            if(a < 0)
                putchar('-'), a *= -a;
    
            if(! a)
                putchar(0);
    
            _print(a);
            putchar('
    ');
        }
    }
    
    struct disjointSet
    {
        int anc[N + 1];
    
        inline void initialize()
        {
            memset(anc, -1, sizeof(anc));
        }
    
        int access(int u)
        {
            return ~ anc[u] ? anc[u] = access(anc[u]) : u;
        }
    
        void link(int u, int pre)
        {
            anc[u] = pre;
        }
    }st;
    
    struct leftistTrees
    {
        struct node
        {
            int suc[2], dis, w;
        }nd[N + 1];
    
        inline void newNode(int u, int w)
        {
            nd[u].w = w, nd[u].dis = 0, nd[u].suc[0] = nd[u].suc[1] = -1;
        }
    
        inline void modify(int u)
        {
            newNode(u, nd[u].w >> 1);
        }
    
        inline int merge(int pre, int u, int v)
        {    
            if(! (~ u))
            {
                if(~ v)
                    st.link(v, pre);
                
                return v;
            }
    
            if(! (~ v))
            {
                if(~ u)
                    st.link(u, pre);
                
                return u;
            }
    
            if(nd[u].w < nd[v].w)
                std::swap(u, v);
    
            nd[u].suc[1] = merge(u, nd[u].suc[1], v);
    
            if(! (~ nd[u].suc[0]) || nd[nd[u].suc[1]].dis > nd[nd[u].suc[0]].dis)
                std::swap(nd[u].suc[0], nd[u].suc[1]);
    
            nd[u].dis = ~ nd[u].suc[1] ? nd[nd[u].suc[1]].dis + 1 : 0;
            st.link(u, pre);
            return u;
        }
    
        inline int pop(int u)
        {
            return merge(-1, nd[u].suc[0], nd[u].suc[1]);
        }
    
        inline int get(int u)
        {
            return nd[u].w;
        }
    }hp;
    
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("HDU1512.in", "r", stdin);
        freopen("HDU1512.out", "w", stdout);
        #endif
    
        using namespace Zeonfai;
    
        while(int n = getInt())
        {
            for(int i = 1; i <= n; ++ i)
                hp.newNode(i, getInt());
        
            int m = getInt();
            st.initialize();
        
            for(int i = 0; i < m; ++ i)
            {
                int u = getInt(), v = getInt(), rtU = st.access(u), rtV = st.access(v);
        
                if(rtU == rtV)
                {
                    puts("-1");
                    continue;
                }
        
                int nwRtU = hp.pop(rtU), nwRtV = hp.pop(rtV);
                hp.modify(rtU), hp.modify(rtV);
                rtU = hp.merge(-1, nwRtU, rtU), rtV = hp.merge(-1, nwRtV, rtV);
                int rt = hp.merge(-1, rtU, rtV);
                println(hp.get(rt));
            }
        }
    }
    
  • 相关阅读:
    CVE-2020-0796 SMBv3 RCE漏洞检测+复现
    Tomcat文件包含漏洞的搭建与复现:CVE-2020-1938
    Web for pentester_writeup之XML attacks篇
    Web for pentester_writeup之LDAP attacks篇
    PhpStudy2018后门漏洞预警及漏洞复现&检测和执行POC脚本
    2016ACM/ICPC亚洲区沈阳站 E
    CodeForces 599C Day at the Beach(贪心)
    CodeForces 652C Foe Pairs(思维题)
    Codeforces 557D. Vitaly and Cycle(二分图判断)
    poj 1091跳蚤(容斥定理+质因子分解)
  • 原文地址:https://www.cnblogs.com/ZeonfaiHo/p/6875404.html
Copyright © 2011-2022 走看看