Hdoj 4010 query on the trees LCT

Source: Internet
Author: User


LCT:

Cut and merge sub-trees. Add a value for the vertex weights of all vertices in the path to query the maximum vertex weights in the path.

Query on the trees Time Limit: 10000/5000 MS (Java/others) memory limit: 65768/65768 K (Java/Others)
Total submission (s): 2582 accepted submission (s): 1208


Problem descriptionwe have met so far 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 shocould solve them efficiently. Wish you have fun!

 
Inputthere 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 shoshould 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 shoshould find the tree in the tree set who contain node X, And You shoshould make the node X be the root of this tree, and then you shoshould 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 shoshould increase their weight by W.
4. Given two integer x, y, you shoshould check the node weights on the path between x and y, And You shoshould output the maximum weight on it.
 
Outputfor each query you shocould output the correct answer of it. If you find this query is an illegal operation, you shocould output-1.
You shoshould output a blank line after each test case.
Sample Input
51 22 42 51 31 2 3 4 564 2 32 1 24 2 31 3 53 2 1 44 1 4
 
Sample output
3-17HintWe 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.  
 
Sourcethe 36th ACM/ICPC Asia Regional Dalian site -- online contest

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=330000;int ch[maxn][2],pre[maxn],key[maxn];int add[maxn],rev[maxn],Max[maxn];bool rt[maxn];void update_add(int r,int d){  if(!r) return ;  key[r]+=d;  add[r]+=d;  Max[r]+=d;}void update_rev(int r){  if(!r) return ;  swap(ch[r][0],ch[r][1]);  rev[r]^=1;}void push_down(int r){  if(add[r])    {      update_add(ch[r][0],add[r]);      update_add(ch[r][1],add[r]);      add[r]=0;    }  if(rev[r])    {      update_rev(ch[r][0]);      update_rev(ch[r][1]);      rev[r]=0;    }}void push_up(int r){  Max[r]=max(max(Max[ch[r][0]],Max[ch[r][1]]),key[r]);}void Rotate(int x){  int y=pre[x],kind=(ch[y][1]==x);  ch[y][kind]=ch[x][!kind];  pre[ch[y][kind]]=y;  pre[x]=pre[y];  pre[y]=x;  ch[x][!kind]=y;  if(rt[y]) rt[y]=false,rt[x]=true;  else ch[pre[x]][ch[pre[x]][1]==y]=x;  push_up(y);}void P(int r){  if(!rt[r]) P(pre[r]);  push_down(r);}void Splay(int r){  P(r);  while(!rt[r])    {      int f=pre[r],ff=pre[f];      if(rt[f]) Rotate(r);      else if((ch[ff][1]==f)==(ch[f][1]==r)) Rotate(f),Rotate(r);      else Rotate(r),Rotate(r);    }  push_up(r);}int Access(int x){  int y=0;  for(;x;x=pre[y=x])    {      Splay(x);      rt[ch[x][1]]=true; rt[ch[x][1]=y]=false;      push_up(x);    }  return y;}bool judge(int u,int v){  while(pre[u]) u=pre[u];  while(pre[v]) v=pre[v];  return u==v;}void mroot(int r){  Access(r);  Splay(r);  update_rev(r);}void lca(int &u,int &v){  Access(v); v=0;  while(u)    {      Splay(u);      if(!pre[u]) return ;      rt[ch[u][1]]=true;      rt[ch[u][1]=v]=false;      push_up(u);      u=pre[v=u];    }}void link(int u,int v){  if(judge(u,v))    {      puts("-1");      return ;    }  mroot(u);  pre[u]=v;}void cut(int u,int v){  if(u==v||!judge(u,v))    {      puts("-1");      return ;    }  mroot(u);  Splay(v);  pre[ch[v][0]]=pre[v];  pre[v]=0;  rt[ch[v][0]]=true;  ch[v][0]=0;  push_up(v);}void Add(int u,int v,int w){  if(!judge(u,v))    {      puts("-1"); return ;    }  lca(u,v);  update_add(ch[u][1],w);  update_add(v,w);  key[u]+=w;  push_up(u);}void query(int u,int v){  if(!judge(u,v))    {      puts("-1");      return ;    }  lca(u,v);  printf("%d\n",max(max(Max[v],Max[ch[u][1]]),key[u]));}struct Edge{  int to,next;}edge[maxn*2];int Adj[maxn],Size=0;void init(){  memset(Adj,-1,sizeof(Adj)); Size=0;}void add_edge(int u,int v){  edge[Size].to=v;  edge[Size].next=Adj[u];  Adj[u]=Size++;}void dfs(int u){  for(int i=Adj[u];~i;i=edge[i].next)    {      int v=edge[i].to;      if(pre[v]!=0) continue;      pre[v]=u;      dfs(v);    }}int n;int main(){  while(scanf("%d",&n)!=EOF)    {      init();      for(int i=0;i<n+10;i++)        {          pre[i]=0; ch[i][0]=ch[i][1]=0;          rev[i]=0; add[i]=0; rt[i]=true;        }      for(int i=0;i<n-1;i++)        {          int u,v;          scanf("%d%d",&u,&v);          add_edge(u,v);          add_edge(v,u);        }      pre[1]=-1; dfs(1); pre[1]=0;      for(int i=1;i<=n;i++)        {          scanf("%d",key+i);          Max[i]=key[i];        }      int q;      scanf("%d",&q);      while(q--)        {          int op;          scanf("%d",&op);          if(op==1)            {              int x,y;              scanf("%d%d",&x,&y);              link(x,y);            }          else if(op==2)            {              int x,y;              scanf("%d%d",&x,&y);              cut(x,y);            }          else if(op==3)            {              int x,y,w;              scanf("%d%d%d",&w,&x,&y);              Add(x,y,w);            }          else if(op==4)            {              int x,y;              scanf("%d%d",&x,&y);              query(x,y);            }        }      putchar(10);    }  return 0;}




Hdoj 4010 query on the trees LCT

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.