HDU4010 Query on The Trees 动态树

我只能说忽略了细节啊。

大家第一次写动态树的时候注意了。由于ACCESS的时候改变一个节点的左右儿子的值,但是但是你左右儿子的父节点的值时不会被修改的!!!!!!!

所以rotate这么写的人太注意了,一个else WA了一整天啊啊


 if(node[y].f)
 {
       if(node[node[y].f].ch[0]==y)
             node[node[y].f].ch[0]=x;
        else //这里不能用else 你的父节点当前未必与你相连,即x未必在PE边上
             node[node[y].f].ch[1]=x;
  }


然后这道题目就是一道裸的动态树了

纪念一下,第一道动态树。。 泪奔,,终于写出一个了。


Query on The Trees

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


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<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<cstdio>

using namespace std;

#define MAXN 350000

int sz;
int ch[MAXN][2],f[MAXN];
int rev[MAXN],lz[MAXN],node[MAXN];
int mx[MAXN];


void newnode(int keys)
{
    int p=++sz;
    mx[p]=node[p]=keys;
    rev[p]=lz[p]=ch[p][0]=ch[p][1]=f[p]=0;
}

 bool isroot(int x)
{
    return (!f[x]||ch[f[x]][0]!=x&&ch[f[x]][1]!=x);
}

void add(int rt,int w)
{
    mx[rt]+=w;
    lz[rt]+=w;
    node[rt]+=w;
}
void push_up(int rt)
{
    if(!rt) return ;
    int l=ch[rt][0];
    int r=ch[rt][1];
    mx[rt]=node[rt];
    if(l) mx[rt]=max(mx[rt],mx[l]);
    if(r) mx[rt]=max(mx[rt],mx[r]);
}

void push_down(int rt)
{
    if(!rt) return;
    if(rev[rt])
    {
        swap(ch[rt][0],ch[rt][1]);
        rev[ch[rt][0]]^=1;
        rev[ch[rt][1]]^=1;
        rev[rt]=0;
    }
    if(lz[rt])
    {
        int l=ch[rt][0];
        int r=ch[rt][1];
        if(l) add(l,lz[rt]);
        if(r) add(r,lz[rt]);
        lz[rt]=0;
    }
}

void rotate(int x,int c)
{
    if(isroot(x)) return;
    int y=f[x],z=f[y];
    ch[y][!c]=ch[x][c];
    if(ch[x][c]) f[ch[x][c]]=y;
    ch[x][c]=y;
    if(f[y])
    {
        if(ch[f[y]][0]==y)
            ch[f[y]][0]=x;
        //这里和一般的SPLAY不一样,注意,不能用else WA出翔了
        //主要是因为,ACCESS的时候ch【x】【i】在不断修改但是f【x】是不变的
        if(ch[f[y]][1]==y)
            ch[f[y]][1]=x;
    }
    f[y]=x;f[x]=z;
    push_up(y);
}

void splay(int x)
{
    int y,z;
    push_down(x);
    while(!isroot(x))
    {
        y=f[x],z=f[y];
        if(isroot(y))
        {
            push_down(y);push_down(x);
            if(ch[y][0]==x)
                rotate(x,1);
            else
                rotate(x,0);
        }
        else
        {
            push_down(z);push_down(y);push_down(x);
            if(ch[z][0]==y)
            {
                if(ch[y][0]==x)
                    rotate(y,1),rotate(x,1);
                else
                    rotate(x,0),rotate(x,1);
            }
            else
            {
                if(ch[y][1]==x)
                    rotate(y,0),rotate(x,0);
                else
                    rotate(x,1),rotate(x,0);
            }
        }
    }
    push_up(x);
}

int Access(int u)
{
    int v=0;
    for(;u;u=f[u])
    {
        splay(u);
        ch[u][1]=v;
        push_up(v=u);
    }
    return v;
}

void makeRoot(int u)
{
    int p=Access(u);
    rev[p]^=1;
    splay(u);
    //push_up(u);
}

int findRoot(int x) {
    for (x = Access(x); push_down(x),ch[x][0]!=0 ; x = ch[x][0]);
    splay(x);
    return x;
}
bool link(int u,int v)
{
	if(findRoot(u)==findRoot(v)) return false;
	makeRoot(u);
	f[u]=v;
	Access(u);
	return true;
}

bool cut(int u,int v)
{
	if(u==v||findRoot(u)!=findRoot(v)) return false;
	makeRoot(u);
	Access(v);
	splay(v);
	f[ch[v][0]]=0;
	ch[v][0]=0;
	push_up(v);
	return true;
}

bool modify(int u,int v,int d) {
    if (findRoot(u)!=findRoot(v)) return false;
    makeRoot(u);
    Access(v);
    splay(v);
    add(v,d);
    return true;
}

bool query(int u,int v,int &ans) {
    if (findRoot(u)!=findRoot(v)) return false;
    makeRoot(u);
    Access(v);
    splay(v);
    ans=mx[v];
    return true;
}


int l[MAXN],r[MAXN],a[MAXN],n;

int main()
{
    while(~scanf("%d",&n))
    {
        sz=0;
        for(int i=0;i<n-1;i++)
            scanf("%d%d",&l[i],&r[i]);
        for(int i=1;i<=n;i++)
        {
            int tmp;
            scanf("%d",&tmp);
            newnode(tmp);
        }
        for(int i=0;i<n-1;i++)
            link(l[i],r[i]);
        int m;
        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            int tp,u,v,w;
            scanf("%d",&tp);
            if(tp==1)
            {
                scanf("%d%d",&u,&v);
                if(!link(u,v)) printf("-1\n");
            }
            else if(tp==2)
            {
                scanf("%d%d",&u,&v);
                if(!cut(u,v)) printf("-1\n");
            }
            else if(tp==3)
            {
                scanf("%d%d%d",&w,&u,&v);
                if(!modify(u,v,w)) printf("-1\n");
            }
            else if(tp==4)
            {
                scanf("%d%d",&u,&v);
                if(!query(u,v,w)) printf("-1\n");
                else printf("%d\n",w);
            }
        }
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值