hdu4010(动态树水题)

Query on The Trees

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


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

题意:给定一棵树,支持4种操作

1 x y 将x所在的树与y所在的树合并

2 x y 如果y和x在同一棵树里,将y以及y的子树分离出来

3 w x y 将x到y路径上各个节点的权值+w

4 x y 查询x到y路径上节点的最大权值

操作之前先检查该操作的合法性,否则输出-1

思路:动态树入门题~~

第一次写动态树的题目,还没晃过神来,改天有时间再补上动态树的知识吧

现在只有代码了sad~~

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         using namespace std; #define maxn 300010 #define FOR(i,n) for(int i=0;i 
        
          y?x:y; } struct node{ ll w; node *fa; node *p; node *ch[2]; ll maxi; int flag; ll cnt; bool root; node(){ w=maxi=flag=cnt=root=0; } int d(){ return this==p->ch[1]; } void setchild(node*c,int d){ ch[d]=c; c->p=this; } void rev(){ flag^=1; swap(ch[0],ch[1]); } void pushup(); void add(ll cn) { cnt+=cn; w+=cn; maxi+=cn; } void isroot(); }*null,*vex[maxn]; void node::isroot() { root=true; p=null; } void node::pushup(){ maxi=w; if(ch[1]!=null)maxi=maxii(w,ch[1]->maxi); if(ch[0]!=null)maxi=maxii(maxi,ch[0]->maxi); } void pushdown(node *o) { if(o->cnt) { FOR(i,2)if (o->ch[i] != null)o->ch[i]->add(o->cnt); o->cnt=0; } if(o->flag) { FOR(i,2)if (o->ch[i] != null)o->ch[i]->rev(); o->flag=0; } } node * newnode(ll w) { node *u=(node*)malloc(sizeof(node)); u->ch[0]=u->ch[1]=u->p=u->fa=null; u->w=w; u->flag=0; u->cnt=0; u->maxi=w; u->root=true; return u; } void pushto(node *o) { static node *stk[maxn]; int rear=0; while(o!=null) { stk[rear++]=o; o=o->p; } for(int i=rear-1;i>=0;i--)pushdown(stk[i]); } void rot(node*o) { node*k=o->p; pushdown(k); pushdown(o); int d=o->d(); k->p->setchild(o,k->d()); k->setchild(o->ch[d^1],d); o->setchild(k,d^1); k->pushup(); if(k->root) { k->root=false; o->root=true; o->fa=k->fa; k->fa=null; } } void splay(node *o) { pushto(o); while(o->p!=null) { if(o->p->p==null)rot(o); else o->p->d()==o->d()?(rot(o->p) , rot(o)):(rot(o) , rot(o)); } o->pushup(); } node* access(node *o) { node *c=null; for(;o!=null;c=o,o=o->fa) { splay(o); o->ch[1]->fa=o; o->ch[1]->isroot(); o->setchild(c,1); c->root=false; } return c; } void makeroot(node*o) { access(o); splay(o); o->rev(); } void link(node *u,node *v) { if(u==v){ printf("-1\n"); return; } makeroot(u); access(v); splay(v); if(u->p==null)u->fa=v; else printf("-1\n"); } void cut(node *u,node *v) { makeroot(u); access(v); splay(v); if(u->p!=null){ v->ch[0]->isroot(); v->ch[0]->fa=null; v->ch[0]=null; v->isroot(); v->fa=null; v->pushup(); } else printf("-1\n"); } void addw(node *u,node *v,ll w) { makeroot(u); access(v); splay(v); if(u==v||u->p!=null)v->add(w); else printf("-1\n"); } void queryw(node *u,node *v) { makeroot(u); access(v); splay(v); if(u==v||u->p!=null)printf("%I64d\n",v->maxi); else printf("-1\n"); } int head[maxn]; int next[maxn<<1]; int edge[maxn<<1]; int d; void dfs(int u,int pre) { int i; for(i=head[u];i!=-1;i=next[i])if(edge[i]!=pre){ vex[edge[i]]->fa=vex[u]; dfs(edge[i],u); } } void add(int u,int v) { edge[d]=v; next[d]=head[u]; head[u]=d++; } int main() { int i,u,v,Q; ll w; null=(node*)malloc(sizeof(node)); null->ch[0]=null->ch[1]=NULL; while(scanf("%d",&n)!=EOF) { d=0; memset(head,-1,sizeof(head)); for(i=1;i 
          
         
       
      
      
     
     
    
    
   
   
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值