hdu4010

http://www.elijahqi.win/archives/753
Problem Description
We have met so many problems on the tree, so today we will have a query problem on a set of trees.
There are N nodes, each node will have a unique weight Wi. We will have four kinds of operations on it and you should solve them efficiently. Wish you have fun!

Input
There are multiple test cases in our dataset.
For each case, the first line contains only one integer N.(1 ≤ N ≤ 300000) The next N‐1 lines each contains two integers x, y which means there is an edge between them. It also means we will give you one tree initially.
The next line will contains N integers which means the weight Wi of each node. (0 ≤ Wi ≤ 3000)
The next line will contains an integer Q. (1 ≤ Q ≤ 300000) The next Q lines will start with an integer 1, 2, 3 or 4 means the kind of this operation.
1. Given two integer x, y, you should make a new edge between these two node x and y. So after this operation, two trees will be connected to a new one.
2. Given two integer x, y, you should find the tree in the tree set who contain node x, and you should make the node x be the root of this tree, and then you should cut the edge between node y and its parent. So after this operation, a tree will be separate into two parts.
3. Given three integer w, x, y, for the x, y and all nodes between the path from x to y, you should increase their weight by w.
4. Given two integer x, y, you should check the node weights on the path between x and y, and you should output the maximum weight on it.

Output
For each query you should output the correct answer of it. If you find this query is an illegal operation, you should output ‐1.
You should output a blank line after each test case.

Sample Input
5 1 2 2 4 2 5 1 3 1 2 3 4 5 6 4 2 3 2 1 2 4 2 3 1 3 5 3 2 1 4 4 1 4

Sample Output
3 -1 7

Hint
We define the illegal situation of different operations: In first operation: if node x and y belong to a same tree, we think it’s illegal. In second operation: if x = y or x and y not belong to a same tree, we think it’s illegal. In third operation: if x and y not belong to a same tree, we think it’s illegal. In fourth operation: if x and y not belong to a same tree, we think it’s illegal.

题解大概看注释就好了

#include<cstdio> 
#include<cstring>
#define N 300005
using namespace std;
struct node{
    int y,next;
}data[N<<1];
int h[N],q[N];
int read(){//读入优化 
    int x=0;char ch=getchar();
    while(ch<'0'||ch>'9') ch=getchar();
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x;
}
int n,m,tmp1,top;
int child[N][2];
int value[N],max[N],father[N],tag[N],rev[N]; // tag为添加操作的懒标记  
inline int max1(int x,int y){
    return x>y?x:y;
}
inline void swap(int &x,int &y){
    int t=x;x=y;y=t;
}
void updata(int x){
    int l=child[x][0],r=child[x][1];
    max[x]=max1(max[l],max[r]);
    max[x]=max1(max[x],value[x]);
}
void pushdown(int x){
    int l=child[x][0],r=child[x][1];
    if (rev[x]){
        rev[x]^=1;rev[l]^=1;rev[r]^=1;swap(child[x][0],child[x][1]);
    }
    if(tag[x]){
        if (l){
            tag[l]+=tag[x];max[l]+=tag[x];value[l]+=tag[x];
        }
        if (r){
            tag[r]+=tag[x];max[r]+=tag[x];value[r]+=tag[x];
        }
        //修改完 tag[x]注意改为0 
        tag[x]=0;
    }
}
bool isroot(int x){
    return (child[father[x]][0]!=x&&child[father[x]][1]!=x);
}
void print(int x){
    if (child[x][0]) print(child[x][0]);
    printf("%d: %d %d %d %d\n",x,value[x],father[x],tag[x],rev[x]);
    if (child[x][1]) print(child[x][1]);
}
void rotate(int x){
    int y=father[x],z=father[y],l,r;
    l=(child[y][1]==x );r=l^1;  //x写成y 小错不断 
    if (!isroot(y))child[z][child[z][1]==y]=x;
    father[child[x][r]]=y;father[y]=x;father[x]=z;
    child[y][l]=child[x][r];child[x][r]=y;
    updata(y);updata(x);
} 
void splay(int x){
    top=0;q[++top]=x;int i=x;
    for (int i=x;!isroot(i);i=father[i]) q[++top]=father[i];
    while(top) pushdown(q[top--]);
    while (!isroot(x)){
        int y=father[x],z=father[y];
        if (!isroot(y)) if (child[y][1]==x^child[z][1]==y) rotate(x);else rotate(y);
        rotate(x);
    }
}
void access(int x){
    for (int t=0;x;t=x,x=father[x])
        splay(x),child[x][1]=t,updata(x);
}
void link(int x,int y){
    //1、将x打通到真正的树根 
    //2、旋转x到 以深度的splay树根  给予旋转标记  
    //3、将x的父节点标记为y 
    access(x);splay(x);rev[x]^=1;father[x]=y; 
} 
int find(int x){
    access(x);splay(x);
    //print(x);printf("\n");
    while(child[x][0]) x=child[x][0];
    return x;
} 
void cut(int x,int y){
    access(x);splay(x);rev[x]^=1;
    splay(y);
    father[child[y][0]]=father[y];
    father[y]=child[y][0]=0;
    updata(y);
}
void add(int x,int y,int w){
    access(x);splay(x);rev[x]^=1;
    access(y);splay(y);tag[y]+=w;value[y]+=w;max[y]+=w;
}
int main(){
    freopen("hdu4010.in","r",stdin);
    freopen("hdu4010.out","w",stdout);
    while(scanf("%d",&n)>0){
        memset(h,0,sizeof(h));
        for (int i=0;i<=n;++i) tag[i]=rev[i]=father[i]=child[i][0]=child[i][1]=0;
        max[0]=-0x7f7f7f7f;tmp1=0;
        for (int i=1;i<n;++i) {
            int x=read(),y=read();
            data[++tmp1].y=y;data[tmp1].next=h[x];h[x]=tmp1;
            data[++tmp1].y=x;data[tmp1].next=h[y];h[y]=tmp1;
        }
        for (int i=1;i<=n;++i) max[i]=value[i]=read();
        q[++top]=1;
        for (int j=1;j<=top;++j){
            int now=q[j];
            for (int i=h[now];i;i=data[i].next){
                int y=data[i].y;
                if (y!=father[now]){
                    father[y]=now;
                    q[++top]=y;
                }
            }
        } //递推 虚边建树 
        m=read();
        //for (int i=1;i<=n;++i) printf("%d ",father[i]); 
        while(m--){
            int op,x,y,w;
            op=read();x=read();y=read();
            //scanf("%d%d%d",&op,&x,&y);
            if (op==1){
                if (find(x)==find(y)) printf("-1\n");else link(x,y);continue;
            }
            if (op==2){
                if (find(x)!=find(y)||x==y) printf("-1\n");else cut(x,y);continue;
            }
            if (op==3){
                w=x;x=y;y=read();
                if (find(x)!=find(y)) printf("-1\n");else add(x,y,w);continue;
            }
            if (op==4){
                if (find(x)!=find(y)) printf("-1\n");else {
                    //打通x到真正树根 同时将比x大的边转换为虚边 标记x  
                    //打通y到真正树根 断开比y小的边  将y变成splay树根 y的max即为所求 
                    access(x);splay(x);rev[x]^=1;access(y);splay(y);printf("%d\n",max[y]);
                }
            }
            //if (m==5) return 0;
        }
        printf("\n"); 
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值