zoukankan      html  css  js  c++  java
  • bzoj 2733: [HNOI2012]永无乡

    2733: [HNOI2012]永无乡

    Time Limit: 10 Sec  Memory Limit: 128 MB
    Submit: 3784  Solved: 2025
    [Submit][Status][Discuss]

    Description

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

    Input

    输入文件第一行是用空格隔开的两个正整数 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 
     

    Output

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

    Sample Input

    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

    Sample Output

    -1
    2
    5
    1
    2

    HINT

     

    Source

    splay+并查集

    每次进行节点插入与合并

    不过,洛谷splay好像过不了QAQ

    会tle一个点

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int maxn = 600007;
    int n,m,q; 
    int val[maxn],fa[maxn],ch[maxn][2],father[maxn],que[maxn],siz[maxn];
    inline void read(int &x)
    {
        x=0;char c=getchar();
        while(c<'0'||c>'9')c=getchar();
        while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
    }
    inline int find(int x)
    {
        if(x!=father[x])father[x]=find(father[x]);
        return father[x];
    }
    inline void pushup(int x) 
    {
        if(x)
        siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;
    }
    inline int son(int x)
    {
        return x==ch[fa[x]][1];
     } 
    inline void rotate(int x)
    {
        int y=fa[x],z=fa[y],b=son(x),c=son(y),a=ch[x][!b];
        if(z)ch[z][c]=x;fa[x]=z;
        if(a)fa[a]=y;ch[y][b]=a;
        ch[x][!b]=y;fa[y]=x;
        pushup(y);
    }
    inline void splay(int x,int i)
    {
        while(fa[x]!=i)
        {
            int y=fa[x];int z=fa[y];
            if(i==z)rotate(x);
            else {
                if(son(y)==son(x))rotate(y),rotate(x);
                else rotate(x),rotate(x);
            }
        }
        pushup(x);
    }
    inline void insert(int &tx,int pre,int id)
    {
        if(!tx){
            tx=id;fa[tx]=pre;siz[tx]=1;
            splay(tx,0);
            return ;    
        }
        if(val[id]<=val[tx])insert(ch[tx][0],tx,id);
        else insert(ch[tx][1],tx,id);
        pushup(tx);
     } 
    inline void merge(int x,int y)
    {
        splay(x,0),splay(y,0);
        if(siz[x]>siz[y])swap(x,y);
        int head=0,tail=1;
        que[head]=y,que[tail]=x;
        while(head<tail)
        {
            int a=que[++head];
            if(ch[a][0])que[++tail]=ch[a][0];
            if(ch[a][1])que[++tail]=ch[a][1];
            ch[a][0]=ch[a][1]=0;
            insert(que[head-1],0,a);
        }
    }
    inline int getkth(int rt,int k)
    {
        if(k<0||k>siz[rt]) return -1;
        while(k!=siz[ch[rt][0]]+1)
        {
            if (k<=siz[ch[rt][0]]) rt=ch[rt][0];
            else k-=siz[ch[rt][0]]+1,rt=ch[rt][1];;
        }
        splay(rt,0);
        return rt;
    }
    int main()
    {
        read(n);read(m);
        for(int i=1;i<=n;i++)read(val[i]),father[i]=i,siz[i]=1;
        int a,b;
        for(int i=1;i<=m;++i){
            read(a),read(b);
            int p=find(a),q=find(b);
            if(p!=q){
                merge(a,b);
                father[p]=q;
            }
        }
        read(q);
        char s[4];
        while(q--)
        {
            scanf("%s",s);
            read(a),read(b);
            if(s[0]=='Q')
            {
                splay(a,0);
                printf("%d
    ",getkth(a,b));
            }
            else 
            {
                int p=find(a),q=find(b);
                if(p!=q)
                {
                    merge(a,b);
                    father[p]=q;
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    Vue(小案例_vue+axios仿手机app)_go实现退回上一个路由
    nyoj 635 Oh, my goddess
    nyoj 587 blockhouses
    nyoj 483 Nightmare
    nyoj 592 spiral grid
    nyoj 927 The partial sum problem
    nyoj 523 亡命逃窜
    nyoj 929 密码宝盒
    nyoj 999 师傅又被妖怪抓走了
    nyoj 293 Sticks
  • 原文地址:https://www.cnblogs.com/sssy/p/7353448.html
Copyright © 2011-2022 走看看