zoukankan      html  css  js  c++  java
  • 【bzoj2733】[HNOI2012]永无乡

    题目描述:

    永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示。某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达另一个岛。如果从岛 a 出发经过若干座(含 0 座)桥可以到达岛 b,则称岛 a 和岛 b 是连 通的。现在有两种操作:B x y 表示在岛 x 与岛 y 之间修建一座新桥。Q x k 表示询问当前与岛 x连通的所有岛中第 k 重要的是哪座岛,即所有与岛 x 连通的岛中重要度排名第 k 小的岛是哪 座,请你输出那个岛的编号。

    输入:

    “输入文件第一行是用空格隔开的两个正整数 n 和 m,分别 表示岛的个数以及一开始存在的桥数。接下来的一行是用空格隔开的 n 个数,依次描述从岛 1 到岛 n 的重要度排名。随后的 m 行每行是用空格隔开的两个正整数 ai 和 bi,表示一开始就存 在一座连接岛 ai 和岛 bi 的桥。后面剩下的部分描述操作,该部分的第一行是一个正整数 q, 表示一共有 q 个操作,接下来的 q 行依次描述每个操作,操作的格式如上所述,以大写字母 Q 或B 开始,后面跟两个不超过 n 的正整数,字母与数字以及两个数字之间用空格隔开。 对于 20%的数据 n≤1000,q≤1000
    对于 100%的数据 n≤100000,m≤n,q≤300000

    输出:

    对于每个 Q x k 操作都要依次输出一行,其中包含一个整数,表 示所询问岛屿的编号。如果该岛屿不存在,则输出-1。

    样例输入:

    5 1
    4 3 2 5 1
    1 2
    7
    Q 3 2
    Q 2 1
    B 2 3
    B 1 5
    Q 2 1
    Q 2 4
    Q 2 3

    样例输出:

    -1
    2
    5
    1
    2

    题解:

    并查集 + splay(其实所有的平衡树都可以) + 启发式合并

    (题外话:这道题我真的是调得生无可恋啊,调了整整三天,提交了两页才调出来



    代码:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
     
    #ifdef WIN32
        #define LL "%I64d"
    #else
        #define LL "%lld"
    #endif
     
    #ifdef CT
        #define debug(...) printf(__VA_ARGS__)
        #define setfile() 
    #else
        #define debug(...)
        #define filename ""
        #define setfile() freopen(filename".in","r",stdin);freopen(filename".out","w",stdout);
    #endif
     
    #define R register
    #define getc() (S==T&&(T=(S=B)+fread(B,1,1<<15,stdin),S==T)?EOF:*S++)
    #define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
    #define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
    #define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
    #define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
    char B[1<<15],*S=B,*T=B;
    inline int FastIn()
    {
        R char ch;R int cnt=0;R bool minus=0;
        while (ch=getc(),(ch < '0' || ch > '9') && ch != '-') ;
        ch == '-' ?minus=1:cnt=ch-'0';
        while (ch=getc(),ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
        return minus?-cnt:cnt;
    }
    #define maxn 100010
    int val[maxn], Fa[maxn], ch[maxn][2], fa[maxn], size[maxn];
    int Find(R int x){return Fa[x] == x ? x : Fa[x] = Find(Fa[x]);}
    inline void update(R int x)
    {
        R int ls = ch[x][0], rs = ch[x][1];
        size[x] = size[ls] + size[rs] + 1;
    }
    inline void rotate(R int x)
    {
        R int f = fa[x], gf = fa[f], d = (ch[f][1] == x);
        (ch[f][d] = ch[x][d ^ 1]) > 0 ? fa[ch[f][d]] = f : 0;
        (fa[x] = gf) > 0 ? ch[gf][ch[gf][1] == f] = x : 0;
        fa[ch[x][d ^ 1] = f] = x;
        update(f);
    }
    inline void splay(R int x, R int rt)
    {
        while (fa[x] != rt)
        {
            R int f = fa[x], gf = fa[f];
            if (gf != rt) rotate((ch[f][1] == x) ^ (ch[gf][1] == f) ? x : f);
            rotate(x);
        }
        update(x);
    }
    void Insert(R int x, R int f)
    {
        if (val[x] < val[f])
        {
            if (ch[f][0])
                Insert(x, ch[f][0]);
            else
            {
                ch[f][0] = x; fa[x] = f;
            }
        }
        else
        {
            if (ch[f][1])
                Insert(x, ch[f][1]);
            else
            {
                ch[f][1] = x; fa[x] = f;
            }
        }
        update(f);
    }
    void merge(R int a, R int b)
    {
        R int tmp1 = 0, tmp2 = 0;
        splay(a, 0);
        splay(b, 0);
        if (size[a] < size[b])
        {
            if (ch[a][0]) tmp1 = ch[a][0];
            if (ch[a][1]) tmp2 = ch[a][1];
            ch[a][0] = 0;
            ch[a][1] = 0;
            fa[tmp1] = 0;
            fa[tmp2] = 0;
            if (tmp1) merge(b, tmp1);
            if (tmp2) merge(b, tmp2);
            Insert(a, b);
        }
        else
        {
            if (ch[b][0]) tmp1 = ch[b][0];
            if (ch[b][1]) tmp2 = ch[b][1];
            ch[b][0] = 0;
            ch[b][1] = 0;
            fa[tmp1] = 0;
            fa[tmp2] = 0;
            if (tmp1) merge(a, tmp1);
            if (tmp2) merge(a, tmp2);
            Insert(b, a);
        }
    }
    int find(R int x, R int k)
    {
        R int ls = ch[x][0], rs = ch[x][1], lsize = size[ls];
        if (lsize + 1 == k) return x;
        if (lsize >= k) return find(ls, k);
        else return find(rs, k - lsize - 1);
    }
    int main()
    {
        R int n = FastIn(), m = FastIn();
        for (R int i = 1; i <= n; ++i) val[i] = FastIn(), Fa[i] = i, size[i] = 1;
        for (R int i = 1; i <= m; ++i)
        {
            R int a = FastIn(), b = FastIn();
            Fa[Find(a)] = Find(b);
        }
        for (R int i = 1; i <= n; ++i){
            R int f = Find(i);
            if (f != i) Insert(i, f);
        }
        R int q = FastIn();
        for (; q; --q)
        {
            R char opt = getc();
            while (opt != 'Q' && opt != 'B') opt = getc();
            R int a = FastIn(), b = FastIn();
            if (opt == 'B')
            {
                R int f1 = Find(a), f2 = Find(b);
                if (f1!=f2) merge(f1, f2), Fa[f1] = f2;
            }
            else
            {
                R int f = Find(a);
                splay(f, 0);
                if (size[f] < b) puts("-1");
                else printf("%d
    ",find(f,b) );
            }
        }
        return 0;
    }


  • 相关阅读:
    免费的视频、音频转文本
    Errors are values
    Codebase Refactoring (with help from Go)
    Golang中的坑二
    Cleaner, more elegant, and wrong(msdn blog)
    Cleaner, more elegant, and wrong(翻译)
    Cleaner, more elegant, and harder to recognize(翻译)
    vue控制父子组件渲染顺序
    computed 和 watch 组合使用,监听数据全局数据状态
    webstorm破解方法
  • 原文地址:https://www.cnblogs.com/cocottt/p/6765022.html
Copyright © 2011-2022 走看看