bzoj2333[SCOI2011]棘手的操作

全抄hzwer的;
…………………………..
http://hzwer.com/5780.html
但是有几个点还是说一下;
首先,如果两个点已经在一个堆里面了,再合并会出现环;
这个很显然把,但是你会忘了写;
其次,系统栈会炸;

#include<set>
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
multiset<int>st;
struct heap{
    int xx,yy,vv,tag,fa;
}q[3000001];
int n,m,x,y,z,xx,yy,cover;
string s;
int get(int x){
    while(q[x].fa)x=q[x].fa;
    return x;
}
void dfs(int x,int y){
    if(!x)return;
    q[x].vv+=y;
    dfs(q[x].xx,y);
    dfs(q[x].yy,y);
}
int merge(int x,int y){
    if(!x||!y)return x+y;
    if(q[x].vv<q[y].vv)swap(x,y);
    q[x].yy=merge(q[x].yy,y);
    q[q[x].yy].fa=x;
    swap(q[x].xx,q[x].yy);
    return x;
}
int del(int x){
    int t=merge(q[x].xx,q[x].yy),f=q[x].fa;
    if(q[f].xx==x)q[f].xx=t;else q[f].yy=t;
    q[x].fa=q[x].xx=q[x].yy=0;
    q[t].fa=f;
    return get(t);
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&q[i].vv),st.insert(q[i].vv);
    scanf("%d",&m);
    while(m--){
        cin>>s;
        if(s=="U"){
            scanf("%d%d",&x,&y);
            xx=get(x);yy=get(y);
            if(xx==yy)break;
            if(xx==merge(xx,yy))st.erase(st.find(q[yy].vv));else st.erase(st.find(q[xx].vv));
        }else
        if(s=="A1"){
            scanf("%d%d",&x,&y);
            xx=get(x);
            st.erase(st.find(q[xx].vv));
            int t=del(x);
            q[x].vv+=y;
            merge(t,x);
            xx=get(x);
            st.insert(q[xx].vv);
        }else
        if(s=="A2"){
            scanf("%d%d",&x,&y);
            xx=get(x);
            st.erase(st.find(q[xx].vv));
            dfs(xx,y);
            st.insert(q[xx].vv);
        }else
        if(s=="A3"){
            scanf("%d",&x);
            cover+=x;
        }else
        if(s=="F1"){
            scanf("%d",&x);
            printf("%d\n",q[x].vv+cover);
        }else
        if(s=="F2"){
            scanf("%d",&x);
            xx=get(x);
            printf("%d\n",q[xx].vv+cover);
        }else
        if(s=="F3")printf("%d\n",*--st.end()+cover);
    }
} 
posted @ 2017-03-02 15:40  largecube233  阅读(171)  评论(0编辑  收藏  举报