bzoj2733 [HNOI2012]永无乡

    xiaoxiao2021-03-25  126

    传送门

    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中较小的那一个中所有的点拆出来加入到另一个splay中

    ps:开始把题读错了QAQ 昨天调试程序花了一晚上,最后有错误一直没有找出来,今早晨各种胡思乱想加了一个“&”结果A了QAQ

    做题得到的启示: 拿到题读懂大体意思后一定要先手推一遍样例,看看自己的理解是否正确【虽然不排除样例有坑的可能性 做题的时候一定要细心,防止出现细小的错误,否则调试起来非常费时间

    #include<cstdio> struct node { int size,num,id; node *ch[2],*fa; void update() { size=ch[0]->size+ch[1]->size+1; } int getwh() { if(fa->ch[0]==this) return 0; return 1; } void setch(int wh,node *child); }pool[100010],*null; void node::setch(int wh,node *child) { ch[wh]=child; if(child!=null) child->fa=this; update(); } node *p[100010]; int f[100010]; int n,m,q,tot,x,y; char c; int find(int n) { if(f[n]!=n) f[n]=find(f[n]); return f[n]; } inline node *getnew(int num,int id) { node *now=pool+ ++tot; now->ch[0]=now->ch[1]=now->fa=null; now->num=num; now->size=1; now->id=id; return now; } inline void rotate(node *now) { node *fa=now->fa,*grand=now->fa->fa; int wh=now->getwh(); fa->setch(wh,now->ch[wh^1]); now->setch(wh^1,fa); now->fa=grand; if(grand!=null) if(grand->ch[0]==fa) grand->ch[0]=now; else grand->ch[1]=now; } inline void splay(node *root,node *now,node *tar) { for(;now->fa!=tar;rotate(now)) if(now->fa->fa!=tar) if(now->getwh()==now->fa->getwh()) rotate(now->fa); else rotate(now); if(tar==null) root=now; } inline void insert(node *root,node *newone) { node *now=root,*last=null; newone->size=1; while(now!=null) { last=now; if(now->num>newone->num) now=now->ch[0]; else now=now->ch[1]; } if(last->num>newone->num) last->setch(0,newone); else last->setch(1,newone); splay(root,newone,null); } inline void del(node *&root) { if(root->ch[0]==null&&root->ch[1]==null) { root=null; return; } if(root->ch[1]==null) { root=root->ch[0]; root->fa=null; return; } if(root->ch[0]==null) { root=root->ch[1]; root->fa=null; return; } node *now=root->ch[0]; while(now->ch[1]!=null) now=now->ch[1]; splay(root,now,root); now->setch(1,root->ch[1]); root=now; } inline void swap(node *&a,node *&b){node *c=a;a=b;b=c;} inline void swapnum(int &a,int &b){int c=a;a=b;b=c;} inline void top(node *&now)//注意取地址符!!! { while(now->fa!=null) now=now->fa; } inline void connect(int x,int y) { node *root1=p[x],*root2=p[y]; top(root1);top(root2); if(root1->size>root2->size) swap(root1,root2),swapnum(x,y); int fy=find(y); while(root1!=null) { node *now=root1; del(root1); insert(root2,now); f[now->id]=fy; p[now->id]=root2; } } inline int kth(int d,int rank) { node *now=p[d]; top(now); if(now->size<rank) return -1; int ranking=0; while(now!=null) { int tmp=ranking+now->ch[0]->size; if(tmp+1==rank) return now->id; if(tmp+1>rank) now=now->ch[0]; else ranking=tmp+1,now=now->ch[1]; } } int main() { scanf("%d%d",&n,&m); null=pool; null->ch[0]=null->ch[1]=null->fa=null; for(int i=1;i<=n;i++) { scanf("%d",&x); p[i]=getnew(x,i); f[i]=i; } for(int i=1;i<=m;i++) { scanf("%d%d",&x,&y); x=find(x);y=find(y); if(x!=y) connect(x,y); } scanf("%d",&q); for(int i=1;i<=q;i++) { c=getchar(); while(c!='Q'&&c!='B') c=getchar(); scanf("%d%d",&x,&y); if(c=='Q') printf("%d\n",kth(x,y)); else { x=find(x);y=find(y); if(f[y]!=x) connect(x,y); } } return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-7966.html

    最新回复(0)