zoukankan      html  css  js  c++  java
  • HDU4010 Query on The Trees

    Query on The Trees

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
    Total Submission(s): 5705    Accepted Submission(s): 2292


    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.
     
    Source
     

    题意:

      给出一颗树,有4种操作:

         1、如果x和y不在同一棵树上则在xy连边

         2、如果x和y在同一棵树上并且x!=y则把x换为树根并把y和y的父亲分离

         3、如果x和y在同一棵树上则x到y的路径上所有的点权值+w

         4、如果x和y在同一棵树上则输出x到y路径上的最大值

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #define m(s) memset(s,0,sizeof s);
    using namespace std;
    const int N=300010;
    const int inf=1000000000;
    using namespace std;
    int fa[N],c[N][2],x[N],y[N],val[N],mx[N],tag[N],rev[N],st[N],n,m;
    inline int read(){
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    void pushdown(int x){
        int l=c[x][0],r=c[x][1];
        if(rev[x]){
            rev[x]^=1;rev[l]^=1;rev[r]^=1;
            swap(c[x][0],c[x][1]);
        }
        if(tag[x]){
            if(l) tag[l]+=tag[x],val[l]+=tag[x],mx[l]+=tag[x];
            if(r) tag[r]+=tag[x],val[r]+=tag[x],mx[r]+=tag[x];
            tag[x]=0;
        }
    }
    void updata(int x){
        int l=c[x][0],r=c[x][1];
        mx[x]=max(val[x],max(mx[l],mx[r]));
    }
    bool isroot(int x){
        return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;
    }
    void rotate(int x){
        int y=fa[x],z=fa[y],l,r;
        l=(c[y][1]==x);r=l^1;
        if(!isroot(y)) c[z][c[z][1]==y]=x;
        fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
        c[y][l]=c[x][r];c[x][r]=y;
        updata(y);updata(x);
    }
    void splay(int x){
        int top=0;st[++top]=x;
        for(int i=x;!isroot(i);i=fa[i]) st[++top]=fa[i];
        for(int i=top;i;i--) pushdown(st[i]);
        while(!isroot(x)){
            int y=fa[x],z=fa[y];
            if(!isroot(y)){
                if((c[y][0]==x)^(c[z][0]==y)) rotate(x);
                else rotate(y);
            }
            rotate(x);
        }
    }
    void access(int x){
        for(int t=0;x;x=fa[x]) splay(x),c[x][1]=t,updata(x),t=x;
    }
    void rever(int x){
        access(x);
        splay(x);
        rev[x]^=1;
    }
    void link(int x,int y){
        rever(x);
        fa[x]=y;
    //    rever(x);
    }
    void cut(int x,int y){
        rever(x);
        access(y);splay(y);
        c[y][0]=fa[c[y][0]]=0;
    //    c[y][0]=fa[x]=0;//TLE*1
        updata(y);
    }
    int find(int x){
        access(x);splay(x);
        int y=x;
        while(c[y][0]) y=c[y][0];
        return y;
    }
    void Clear(){
        m(fa);m(c);m(val);m(mx);m(tag);m(rev);
    }
    int main(){
        while(scanf("%d",&n)==1){                               
            Clear();
            mx[0]=-inf;
            for(int i=1;i<n;i++) x[i]=read(),y[i]=read();
            for(int i=1;i<=n;i++) val[i]=mx[i]=read();
            for(int i=1;i<n;i++) link(x[i],y[i]);
            m=read();
            for(int i=1,opt,x,y,w;i<=m;i++){
                opt=read();
                if(opt==1){
                    x=read();y=read();
                    if(find(x)==find(y)) puts("-1");
                    else link(x,y);
                }
                else if(opt==2){
                    x=read();y=read();
                    if(find(x)!=find(y)||x==y) puts("-1");
                    else cut(x,y);
                }
                else if(opt==3){
                    w=read();x=read();y=read();
                    if(find(x)!=find(y)) puts("-1");
                    else{
                        rever(x);
                        access(y);
                        splay(y);
                        val[y]+=w;
                        tag[y]+=w;
                        mx[y]+=w;
                    }
                }
                else{
                    x=read();y=read();
                    if(find(x)!=find(y)) puts("-1");
                    else{
                        rever(x);
                        access(y);
                        splay(y);
                        printf("%d
    ",mx[y]);
                    }
                }
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    spring事务管理器设计思想(一)
    ThreaLocal内存泄露的问题
    denyhost防止SSH暴力破解
    qt下调用win32api 修改分辨率
    Windows下pip安装包报错:Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat
    Centos 6.5 下安装socket5代理
    Mac 下安装mitmproxy
    Mac OS 下安装wget
    判断客户端是否使用代理服务器及其匿名级别
    Mac OS 下安装rar unrar命令
  • 原文地址:https://www.cnblogs.com/shenben/p/6477241.html
Copyright © 2011-2022 走看看