zoukankan      html  css  js  c++  java
  • hdu 5002 Tree

    Tree

    Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 366    Accepted Submission(s): 163


    Problem Description
    You are given a tree with N nodes which are numbered by integers 1..N. Each node is associated with an integer as the weight.

    Your task is to deal with M operations of 4 types:

    1.Delete an edge (x, y) from the tree, and then add a new edge (a, b). We ensure that it still constitutes a tree after adding the new edge.

    2.Given two nodes a and b in the tree, change the weights of all the nodes on the path connecting node a and b (including node a and b) to a particular value x.

    3.Given two nodes a and b in the tree, increase the weights of all the nodes on the path connecting node a and b (including node a and b) by a particular value d.

    4.Given two nodes a and b in the tree, compute the second largest weight on the path connecting node a and b (including node a and b), and the number of times this weight occurs on the path. Note that here we need the strict second largest weight. For instance, the strict second largest weight of {3, 5, 2, 5, 3} is 3.
     
    Input
    The first line contains an integer T (T<=3), which means there are T test cases in the input.

    For each test case, the first line contains two integers N and M (N, M<=10^5). The second line contains N integers, and the i-th integer is the weight of the i-th node in the tree (their absolute values are not larger than 10^4).

    In next N-1 lines, there are two integers a and b (1<=a, b<=N), which means there exists an edge connecting node a and b.

    The next M lines describe the operations you have to deal with. In each line the first integer is c (1<=c<=4), which indicates the type of operation.

    If c = 1, there are four integers x, y, a, b (1<= x, y, a, b <=N) after c.
    If c = 2, there are three integers a, b, x (1<= a, b<=N, |x|<=10^4) after c.
    If c = 3, there are three integers a, b, d (1<= a, b<=N, |d|<=10^4) after c.
    If c = 4 (it is a query operation), there are two integers a, b (1<= a, b<=N) after c.

    All these parameters have the same meaning as described in problem description.
     
    Output
    For each test case, first output "Case #x:"" (x means case ID) in a separate line.

    For each query operation, output two values: the second largest weight and the number of times it occurs. If the weights of nodes on that path are all the same, just output "ALL SAME" (without quotes).
     
    Sample Input
    2 3 2 1 1 2 1 2 1 3 4 1 2 4 2 3 7 7 5 3 2 1 7 3 6 1 2 1 3 3 4 3 5 4 6 4 7 4 2 6 3 4 5 -1 4 5 7 1 3 4 2 4 4 3 6 2 3 6 5 4 3 6
     
    Sample Output
    Case #1: ALL SAME 1 2 Case #2: 3 2 1 1 3 2 ALL SAME
     
    Source
     

     思路:lct,就是有点繁琐。。==

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    #include<vector>
    #include<set>
    #include<stack>
    #include<map>
    #include<ctime>
    #include<bitset>
    #define LL long long
    #define INF 89898989
    #define mod 20140518
    #define maxn 100010
    using namespace std;
    
    int head[maxn],next1[maxn*2],to[maxn*2] ;
    int top ,n ;
    bool vi[maxn] ;
    
    void Unit(int u,int v )
    {
        next1[top] = head[u] ;to[top] = v ;
        head[u] = top++;
    }
    struct LCT
    {
        int w[maxn],add[maxn],Max1[maxn];
        int ch[maxn][2],pre[maxn] ;
        int set1[maxn],Max2[maxn];
        int num1[maxn],num2[maxn];
        int size1[maxn];
        int rev[maxn] ;
        void init()
        {
            memset(rev,0,sizeof(rev)) ;
            memset(ch,0,sizeof(ch)) ;
            memset(add,0,sizeof(add));
            memset(pre,0,sizeof(pre)) ;
            memset(size1,0,sizeof(size1));
            memset(num2,1,sizeof(num2));
        }
        void update(int x )
        {
            size1[x] = size1[ch[x][1]]+1+size1[ch[x][0]];
            Max1[x] = w[x] ;
            num1[x]=1;
            num2[x]=1;
            Max2[x] = -INF;
            int ls=ch[x][0],rs=ch[x][1];
            hehe(Max1[x],num1[x],Max2[x],num2[x],ls) ;
            hehe(Max1[x],num1[x],Max2[x],num2[x],rs) ;
        }
        void Add(int x ,int val)
        {
            if(x)
            {
                add[x] += val ;
                w[x] += val ;
                Max1[x] += val ;
                if(Max2[x] != -INF)Max2[x] += val ;
            }
        }
        void Set(int x,int val)
        {
            if(x)
            {
                add[x] = 0 ;
                Max1[x] = val ;
                num1[x] = size1[x];
                Max2[x] = -INF ;
                num2[x] = 1;
                set1[x] = val;
                w[x] = val ;
            }
        }
        void down(int x )
        {
            if(set1[x] != INF)
            {
                Set(ch[x][0],set1[x]) ;
                Set(ch[x][1],set1[x]) ;
                set1[x] = INF;
            }
            if(add[x])
            {
                Add(ch[x][0],add[x]) ;
                Add(ch[x][1],add[x]) ;
                add[x]=0;
            }
            if(rev[x])
            {
                swap(ch[x][0],ch[x][1]) ;
                rev[ch[x][0]] ^= 1;
                rev[ch[x][1]] ^= 1;
                rev[x] = 0;
            }
        }
        bool isroot(int x )
        {
            if(ch[pre[x]][0] != x && ch[pre[x]][1] != x )
                return true ;
            return false;
        }
        void pushdown(int x )
        {
            if(!isroot(x)) pushdown(pre[x]) ;
            down(x) ;
        }
        void rotate(int x,int f)
        {
            int y = pre[x],z = pre[y];
            ch[y][!f] = ch[x][f];
            pre[ ch[x][f] ] = y;
            pre[x] = pre[y];
            if(ch[z][0] == y)ch[z][0] = x;
            else if(ch[z][1] == y)ch[z][1] = x;
            pre[y] = x;
            ch[x][f] = y;
            update(y);
        }
        void splay(int x)
        {
            pushdown(x);
            while(!isroot(x))
            {
                if(isroot(pre[x]))rotate(x,ch[pre[x]][0] == x);
                else
                {
                    int y = pre[x],z = pre[y];
                    int f = (ch[z][0] == y);
                    if(ch[y][f] == x)rotate(x,!f),rotate(x,f);
                    else rotate(y,f),rotate(x,f);
                }
            }
            update(x);
        }
        void access(int u)
        {
            for(int f = 0 ; u ;u = pre[u])
            {
                splay(u);
                ch[u][1] = f ;
                update(u);
                f = u ;
            }
        }
        bool check(int a ,int b)
        {
            int x =a,y=b;
            access(y) ;
            for(int f = 0 ;x ;x = pre[x])
            {
                splay(x) ;
                if(!pre[x]) break ;
                f = x ;
            }
            for( ; ch[x][1] ; x = ch[x][1]);
            return x == b ;
        }
        void make_root(int x )
        {
            access(x) ;
            splay(x) ;
            rev[x] ^= 1;
        }
        void cut(int x,int y )
        {
            make_root(x) ;
            splay(y) ;
            pre[ch[y][0]] = pre[y] ;
            pre[y] = ch[y][0] = 0 ;
        }
        void hehe(int &m1,int &n1,int &m2,int &n2,int x )
        {
            if(!x) return ;
            if(Max1[x]==m1)
            {
                n1 += num1[x] ;
                if(Max2[x] > m2)
                {
                    m2 = Max2[x] ;
                    n2 = num2[x] ;
                }
                else if(Max2[x]==m2)
                {
                    n2 += num2[x] ;
                }
            }
            else if(Max1[x] > m1)
            {
                if(m1 == Max2[x])
                {
                    m2 = m1;
                    n2 = n1+num2[x] ;
                }
                else if(m1 > Max2[x])
                {
                    m2 = m1;
                    n2 = n1;
                }
                else
                {
                    m2 = Max2[x] ;
                    n2 = num2[x] ;
                }
                n1 = num1[x] ;
                m1 = Max1[x] ;
            }
            else
            {
                if(m2==Max1[x])
                {
                    n2 += num1[x] ;
                }
                else if(Max1[x] > m2)
                {
                    m2 = Max1[x] ;
                    n2 = num1[x] ;
                }
            }
        }
        int find(int x,int y,int &num)
        {
            access(y) ;
            for( int f = 0 ; x ; x = pre[x])
            {
                splay(x) ;
                if(!pre[x])
                {
                    int n1=1,m1=w[x],m2=-INF,n2=0;
                    hehe(m1,n1,m2,n2,f) ;
                    hehe(m1,n1,m2,n2,ch[x][1]) ;
                    num = n2 ;
                    return m2;
                }
                ch[x][1] = f ;
                f = x ;
                update(x) ;
            }
            return -1;
        }
        void add1(int x ,int y ,int add)
        {
            access(y) ;
            for(int f = 0 ; x ; x = pre[x])
            {
                splay(x) ;
                if(!pre[x])
                {
                    w[x] += add ;
                    Max1[x] += add ;
                    if(Max2[x] != -INF) Max2[x] += add;
                    Add(f,add) ;
                    Add(ch[x][1],add) ;
                    return ;
                }
                ch[x][1] = f ;
                f = x ;
                update(x) ;
            }
        }
        void SET(int x ,int y ,int s)
        {
            access(y) ;
            for(int f = 0 ; x ; x = pre[x])
            {
                splay(x) ;
                if(!pre[x])
                {
                    w[x] = s;
                    set1[x] = INF;
                    Max1[x] = s;
                    num1[x] = size1[x] ;
                    num2[x] = 1;
                    Max2[x] = -INF;
                    Set(f,s) ;
                    Set(ch[x][1],s) ;
                    return ;
                }
                ch[x][1] = f ;
                f = x ;
                update(x) ;
            }
        }
        void link(int x,int y )
        {
            make_root(x) ;
            splay(x);
            pre[x]=y;
        }
    }lct;
    void dfs(int u,int f)
    {
        for(int i = head[u]; i != -1; i=next1[i])
        {
            int v = to[i] ;
            if(v==f) continue ;
            dfs(v,u);
            lct.pre[v] = u;
        }
    }
    int next_int() {
        char ch;
        int res;
        bool neg;
        while (ch = getchar(), !isdigit(ch) && ch != '-')
            ;
        if (ch == '-') {
            neg = true;
            res = 0;
        } else {
            neg = false;
            res = ch - '0';
        }
        while (ch = getchar(), isdigit(ch))
            res = res * 10 + ch - '0';
        return neg ? -res : res;
    }
    int main()
    {
        int n,m,i,j,k,num;
        int T,case1=0,u,v,c ;
       // freopen("in.txt","r",stdin);
        cin >> T ;
        while(T--)
        {
            scanf("%d%d",&n,&m) ;
            lct.init();
            for( i = 1 ; i <= n ;i++)
            {
                lct.w[i] = next_int() ;
                lct.set1[i]=INF;
                lct.Max2[i]=-INF;
            }
            top=0;
            memset(head,-1,sizeof(head)) ;
            for( i = 1 ; i < n ;i++)
            {
                u = next_int();
                v = next_int();
                Unit(u,v) ;
                Unit(v,u) ;
            }
            dfs(1,-1);
            printf("Case #%d:
    ",++case1);
            while(m--)
            {
                //scanf("%d%d%d",&u,&v,&c) ;
                scanf("%d",&k) ;
                if(k==1)
                {
                  //  scanf("%d%d",&u,&v) ;
                    u = next_int();
                    v = next_int();
                    lct.cut(u,v) ;
                   // scanf("%d%d",&u,&v) ;
                    u = next_int();
                    v = next_int();
                    lct.link(u,v) ;
                }
                else if(k==2)
                {
                 //   scanf("%d%d%d",&u,&v,&c) ;
                    u = next_int();
                    v = next_int();
                    c = next_int();
                    lct.SET(u,v,c) ;
                }
                else if(k==3)
                {
                    scanf("%d%d%d",&u,&v,&c) ;
                    lct.add1(u,v,c) ;
                }
                else{
                   // scanf("%d%d",&u,&v) ;
                    u = next_int();
                    v = next_int();
                    j = lct.find(u,v,num) ;
                    if(j==-INF) puts("ALL SAME");
                    else printf("%d %d
    ",j,num);
                }
            }
        }
        return 0 ;
    }
    

      

  • 相关阅读:
    Android Native Hook技术(一)
    Android Native Hook技术(二)
    Dalvik源码阅读笔记(一)
    Dalvik源码阅读笔记(二)
    Android反调试笔记
    /dev/mem可没那么简单
    jenkins使用邮件功能
    docker 安装 oracle
    jstack
    docker network
  • 原文地址:https://www.cnblogs.com/20120125llcai/p/4006563.html
Copyright © 2011-2022 走看看