HDU5002--Tree(LCT)

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
Link-Cut-Tree模板。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 300800
#define inf 0x3f3f3f3f
int first[maxn],nxt[maxn],vv[maxn];
int e;
void Init()
{
    e = 0;
    memset(first,-1,sizeof(first));
}

void addedge(int u,int v)
{
    vv[e] = v;    nxt[e] = first[u];    first[u] = e++;
    vv[e] = u;    nxt[e] = first[v];    first[v] = e++;
}

inline int max(int a,int b)
{
    return a>b?a:b;
}
inline int max(int a,int b,int c)
{
    if(a<b) a = b;
    if(a<c) a = c;
    return a;
}
void scanf_f(int & a)
{
	a = 0;
	bool ok = 0;
	char c;
	while((c = getchar()) < '0' || c > '9')
	{
		if(c == '-')	ok = 1;
	}
	a = c - '0';
	while((c = getchar()) >= '0' && c <= '9')
		a = a*10 + c - '0';
	if(ok)	a = -a;
}
struct Link_Cut_Tree
{
    int key[maxn],pre[maxn],ch[maxn][2],flip[maxn],add[maxn],set[maxn],size[maxn],root[maxn],fa[maxn];
    int maxkey[maxn][2],num[maxn][2],stk[maxn];
    void init()
    {
        root[0] = size[0] = add[0] = ch[0][0] = ch[0][1] = flip[0] = key[0] = pre[0] = fa[0] = 0;
        maxkey[0][0] = maxkey[0][1] = set[0] = -inf;
        num[0][0] = num[0][1] = 0;
    }
    
	void NewNode(int r,int father,int k)
    {
        num[r][0] = 1;num[r][1] = 0;
        maxkey[r][0] = k;    maxkey[r][1] = -inf;
        root[r] = 1;
        key[r] = k;
        size[r] = 1;
        fa[r] = father;
        ch[r][0] = ch[r][1] = add[r] = flip[r] = pre[r] = 0;
        set[r] = -inf;
    }
    
    void PushTo(int x)
    {
        int rear = 0;
        while(x)
        {
            stk[rear++] = x;
            x = pre[x];
        }
        for(int i = rear-1;i >= 0;i--)    PushDown(stk[i]);
    }

    void PushDown(int x)
    {
        if(set[x] != -inf)
        {
            Update_Set(ch[x][0],set[x]);
            Update_Set(ch[x][1],set[x]);
            set[x] = -inf;
        }
        if(add[x])
        {
            Update_Add(ch[x][0],add[x]);
            Update_Add(ch[x][1],add[x]);
            add[x] = 0;
        }
        if(flip[x])
        {
            Update_Rev(ch[x][0]);
            Update_Rev(ch[x][1]);
            flip[x] = 0;
        }
    }

    void PushUp(int x)
    {
        int l = ch[x][0],r = ch[x][1];
        //維護size,cnt,maxkey[x][0],maxkey[x][1],num[x][0],num[x][1].
        size[x] = 1 + size[l] + size[r];
        int a = max(key[x],maxkey[l][0],maxkey[r][0]);
        int numa = 0;
        int b = -inf;
        if(key[x] != a)
            b = max(key[x],b);
        if(maxkey[l][0] == a)
            b = max(maxkey[l][1],b);
        else b = max(maxkey[l][0],b);
        if(maxkey[r][0] == a)
            b = max(maxkey[r][1],b);
        else b = max(maxkey[r][0],b);
        int numb = 0;
        if(key[x] == b)    numb += 1;
        if(maxkey[l][0] == b)    numb += num[l][0];
        if(maxkey[l][1] == b)    numb += num[l][1];
        if(maxkey[r][0] == b)    numb += num[r][0];
        if(maxkey[r][1] == b)    numb += num[r][1];
        if(key[x] == a)    numa += 1;
        if(maxkey[l][0] == a)    numa += num[l][0];
        if(maxkey[r][0] == a)    numa += num[r][0];
        maxkey[x][0] = a;
        maxkey[x][1] = b;
        num[x][0] = numa;
        num[x][1] = numb;
        if(b == -inf)    num[x][1] = 0;
    }

    void Update_Rev(int x)
    {
        swap(ch[x][0],ch[x][1]);
        flip[x] ^= 1;
    }

    void Update_Set(int x,int a)
    {
        if(!x)    return;
        add[x] = 0;
        key[x] = a;
        set[x] = a;
        maxkey[x][0] = a;
        num[x][0] = size[x];
        maxkey[x][1] = -inf;
        num[x][1] = 0;
    }
    
	
	void Update_Add(int x,int a)
    {
        if(!x)    return;
        add[x] += a;
        key[x] += a;
        maxkey[x][0] += a;
        if(maxkey[x][1] != -inf)
            maxkey[x][1] += a;
    }
    
    void Rotate(int x,int kind)
    {
        int y = pre[x];
        //PushDown(y);
        //PushDown(x);
        ch[y][!kind] = ch[x][kind];
        if(ch[x][kind])	
			pre[ch[x][kind]] = y;
        if(pre[y])
            ch[pre[y]][ch[pre[y]][1]==y] = x;
        pre[x] = pre[y];
        ch[x][kind] = y;
        pre[y] = x;
        PushUp(y);
        
        if(root[y])
        {
            root[y] = 0;
            root[x] = 1;
            fa[x] = fa[y];
            fa[y] = 0;
        }    
    }

    void Splay(int r)
    {
        PushTo(r);
        while(pre[r] != 0)
        {
            int y = pre[r],z = pre[y];
            //PushDown(z);    PushDown(y);    PushDown(r);
            if(z == 0)
            {
                Rotate(r,ch[y][0]==r);        
            }
            else 
            {
                int kind = (ch[z][0] == y);
                if(ch[y][kind] == r)
                {
                    Rotate(r,!kind);
                    Rotate(r,kind);
                }
                else 
                {
                    Rotate(y,kind);
                    Rotate(r,kind);
                }
            }
        }
        PushUp(r);
    }
    
    int Access(int x)
    {
        int c = 0;
        while(x)
        {
            Splay(x);
            if(ch[x][1])
            {
                fa[ch[x][1]] = x;
                Is_Root(ch[x][1]);
            }
            ch[x][1] = c;
            if(c)
			{
                pre[c] = x;
				fa[c] = 0;
			}
            root[c] = 0;
            c = x;    x = fa[x];
        }
        return c;
    }

    void Make_Root(int x)
    {
        Access(x);
        Splay(x);
        Update_Rev(x);
    }
    
    void Is_Root(int x)
    {
        root[x] = 1;
        pre[x] = 0;
    }

    void Link(int u,int v)
    {
        if(u == v)    return;
        Make_Root(u);
        Access(v);
        Splay(v);
        if(pre[u] == 0)    fa[u] = v;
    }

    void Cut(int u,int v)
    {
        Make_Root(u);
        Access(v);
        Splay(v);
        if(pre[u])
        {
            if(ch[v][0])
				Is_Root(ch[v][0]);
            fa[ch[v][0]] = 0;
            ch[v][0] = 0;
            Is_Root(v);
            fa[v] = 0;
            PushUp(v);
        }
    }

    void Update_Set_UtoV(int u,int v,int w)
    {
        Make_Root(u);
        Access(v);
        Splay(v);
        if(u == v || pre[u] != 0)    Update_Set(v,w);
    }

    void Update_Add_UtoV(int u,int v,int w)
    {
        Make_Root(u);
        Access(v);
        Splay(v);
        if(u == v || pre[u] != 0)    Update_Add(v,w);
    }

    void Query(int u,int v)
    {
        if(u == v)
        {
            printf("ALL SAME\n");
            return;
        }
        Make_Root(u);
        Access(v);
        Splay(v);
        if(pre[u])
        {
            if(num[v][1]==0)    printf("ALL SAME\n");
            else printf("%d %d\n",maxkey[v][1],num[v][1]);
        }

    }
    void dfs(int u,int pre)
    {
        NewNode(u,pre,key[u]);
        for(int i = first[u];i != -1;i = nxt[i])
        {
            int v = vv[i];
            if(v == pre)    continue;
            dfs(v,u);
        }

    }

    void solve(int n,int m)
    {
        init();
        for(int i = 1;i <= n;i++)
            //scanf("%d",&key[i]);
			scanf_f(key[i]);
        for(int i = 1;i < n;i++)
        {
            int u,v;
            //scanf("%d%d",&u,&v);
			scanf_f(u);
			scanf_f(v);
            addedge(u,v);
        }
        dfs(1,0);
        while(m--)
        {
            int ope;    //scanf("%d",&ope);
			scanf_f(ope);
            if(ope == 1)
            {
                int x,y,a,b;
                //scanf("%d%d%d%d",&x,&y,&a,&b);
				scanf_f(x);
				scanf_f(y);
				scanf_f(a);
				scanf_f(b);
                Cut(x,y);
                Link(a,b);
            }
            else if(ope == 2)
            {
                int a,b,x;
                //scanf("%d%d%d",&a,&b,&x);
				scanf_f(a);
				scanf_f(b);
				scanf_f(x);
                Update_Set_UtoV(a,b,x);
            }
            else if(ope == 3)
            {
                int a,b,d;
                //scanf("%d%d%d",&a,&b,&d);
				scanf_f(a);
				scanf_f(b);
				scanf_f(d);
                Update_Add_UtoV(a,b,d);
            }
            else 
            {
                int a,b;
                //scanf("%d%d",&a,&b);
				scanf_f(a);
				scanf_f(b);
                Query(a,b);
            }
        }

    }
}lct;

int main()
{
    //freopen("in.txt","r",stdin);
    int t,cas = 0;
    scanf_f(t);
	//scanf("%d",&t);
    while(t--)
    {
        Init();
        int n,m;
        //scanf("%d%d",&n,&m);
		scanf_f(n);
		scanf_f(m);
		printf("Case #%d:\n",++cas);
        lct.solve(n,m);
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章