HDOJ question 4010 Query on The Trees (Link Cut Tree connection, edge deletion, path point addition, maximum path point weight), hdoj4010

Source: Internet
Author: User

HDOJ question 4010 Query on The Trees (Link Cut Tree connection, edge deletion, path point addition, maximum path point weight), hdoj4010
Query on The TreesTime Limit: 10000/5000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission (s): 3602 Accepted Submission (s): 1587


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
Recommendlcy | We have carefully selected several similar problems for you: 4004 4005 4007 4001 4008 question: n vertices, n-1 edges, each vertex has a vertex permission, operation 1 connects x y, operation 2 disconnects x y, Operation 3 (first input val) vertices from x to y are added with val, operation 4, calculate the maximum vertex weight on the path x to y. The ac code
It is true that the kuangbin code is faster and runs more than 800 times, but I just don't get used to it. I should use my preferred method...
#include<stdio.h>              #include<string.h>          #include<queue>        #include<iostream>        #define INF 0x7fffffff #define N 300030       #define max(a,b) (a>b?a:b)        using namespace std;      int vis[N];          struct LCT          {              int bef[N],pre[N],next[N][2],key[N],add[N]; int rev[N],maxn[N];    void init()              {          memset(pre,0,sizeof(pre));          memset(next,0,sizeof(next));           rev[0]=rev[1]=0;add[0]=add[1]=0;bef[0]=bef[1]=0;maxn[0]=key[0]=0;    }  void update_add(int x,int val){if(x){add[x]+=val;key[x]+=val;maxn[x]+=val;}}void update_rev(int x){if(!x)return;swap(next[x][0],next[x][1]);rev[x]^=1;}void pushup(int x){maxn[x] = max(key[x], max(maxn[next[x][0]], maxn[next[x][1]]));}    void pushdown(int x)      {          if(add[x]){update_add(next[x][0],add[x]);update_add(next[x][1],add[x]);add[x]=0;}if(rev[x]){update_rev(next[x][0]);update_rev(next[x][1]);//swap(next[x][0],next[x][1]);rev[x]=0;}    }      void rotate(int x,int kind)              {                  int y,z;                  y=pre[x];                  z=pre[y];          pushdown(y);          pushdown(x);          next[y][!kind]=next[x][kind];                  pre[next[x][kind]]=y;                  next[z][next[z][1]==y]=x;                  pre[x]=z;                  next[x][kind]=y;                  pre[y]=x;          pushup(y);    }              void splay(int x)              {                  int rt;                  for(rt=x;pre[rt];rt=pre[rt]);                  if(x!=rt)                  {                      bef[x]=bef[rt];                      bef[rt]=0;    pushdown(x);              while(pre[x])                      {                          if(next[pre[x]][0]==x)                          {                              rotate(x,1);                          }                          else                            rotate(x,0);                      }   pushup(x);        }     }              void access(int x)              {                  int fa;                  for(fa=0;x;x=bef[x])                  {                      splay(x);   pushdown(x);              pre[next[x][1]]=0;                      bef[next[x][1]]=x;                      next[x][1]=fa;                      pre[fa]=x;                      bef[fa]=0;                      fa=x;   pushup(x);        }              }  int getroot(int x){access(x);splay(x);while(next[x][0])x=next[x][0];return x;}void makeroot(int x){access(x);splay(x);update_rev(x);}void link(int x,int y){makeroot(x);makeroot(y);bef[x]=y;}void cut(int y,int x){makeroot(y);access(x);splay(x);bef[next[x][0]]=bef[x];bef[x]=0;pre[next[x][0]]=0;next[x][0]=0;pushup(x);}    void change(int x,int y,int val)          {             access(y);              for(y=0;x;x=bef[x])              {                  splay(x);                  if(!bef[x])                  {                       key[x]+=val;                   update_add(y,val); update_add(next[x][1],val);                 return;              }     pushdown(x);              pre[next[x][1]]=0;                  bef[next[x][1]]=x;                  next[x][1]=y;                  pre[y]=x;                  bef[y]=0;                  y=x;  pushup(x);        }             }          int query(int x,int y)          {             access(y);              for(y=0;x;x=bef[x])              {                  splay(x);                  if(!bef[x])                  {                       return max(key[x],max(maxn[next[x][1]],maxn[y]));            }     pushdown(x);              pre[next[x][1]]=0;                  bef[next[x][1]]=x;                  next[x][1]=y;                  pre[y]=x;                  bef[y]=0;                  y=x;  pushup(x);        }             }      }lct;      struct s      {          int u,v,next;      }edge[N<<1];      int head[N],cnt;      void add(int u,int v)      {          edge[cnt].u=u;          edge[cnt].v=v;          edge[cnt].next=head[u];          head[u]=cnt++;      }      void bfs(int u)         {                           queue<int>q;                  memset(vis,0,sizeof(vis));                  vis[u]=1;                  q.push(u);                  while(!q.empty())                  {                          u=q.front();                          q.pop();                          for(int i=head[u];i!=-1;i=edge[i].next)                          {                                  int v=edge[i].v;                                  if(!vis[v])                                  {                                          lct.bef[v]=u;                                                        vis[v]=1;                                          q.push(v);                                  }                          }                  }          }    int main(){int n;while(scanf("%d",&n)!=EOF){int i;cnt=0;memset(head,-1,sizeof(head));for(i=1;i<n;i++){int u,v;scanf("%d%d",&u,&v);add(u,v);add(v,u);}lct.init();for(i=1;i<=n;i++){scanf("%d",&lct.key[i]);}bfs(1);int q;scanf("%d",&q);while(q--){int op,x,y;scanf("%d%d%d",&op,&x,&y);if(op==1){if(lct.getroot(x)==lct.getroot(y)){printf("-1\n");}elselct.link(x,y);}elseif(op==2){if(x==y||lct.getroot(x)!=lct.getroot(y)){printf("-1\n");}elselct.cut(x,y);}elseif(op==3){int z;scanf("%d",&z);if(lct.getroot(y)!=lct.getroot(z)){printf("-1\n");}elselct.change(y,z,x);}else{if(lct.getroot(x)!=lct.getroot(y)){printf("-1\n");}elseprintf("%d\n",lct.query(x,y));}}printf("\n");}}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.