Bzoj3639:query on a tree VII

Source: Internet
Author: User

Descriptionyou is given a tree (an acyclic undirected connected graph) with n nodes. The tree nodes is numbered from 1 to n.

Each node have a color, white or black, and a weight.

We'll ask you to perfrom some instructions of the following form:

    • 0 u : Ask for the maximum weight among the nodes which is connected to u, both nodes is connect Ed IFF all the node on the path from u to v (inclusive u and v) H Ave a same color.
    • 1 u : Toggle the color of u(that's, from black to white, or from white to black).
    • 2 u w: Change the weight of u to w.

Input

The first line contains a number n denoted how many nodes in the tree (1≤ n ≤105). The next n -1 lines, each line has a numbers (u, v) describe a edge of the tree ( 1≤ u, vn).

The next 2 lines, each line contains n number, the first line was the initial color of each node (0 or 1), a nd the second line was the initial weight, let's say Wi, of each node (| Wi|≤109).

The next line contains a number m denoted how many operations we is going to process (1≤ m ≤10 5). The next m lines, each line describe a operation (t, u) as we mentioned above ( 0≤ t ≤2, 1≤ un, |  W| ≤109).

Output

For each query operation, output the corresponding result.

Sample Input5
1 2
1 3
1 4
1 5
0 1 1) 1 1
1 2 3) 4 5
3
0 1
1 1
0 1

Sample Output1
5
The http://www.cnblogs.com/chenyushuo/p/5228875.html is similar to this one, but to maintain the maximum weight of points connected by virtual edges with set, LCT maintain the maximum value on this chain. Code
1#include <cstdio>2#include <iostream>3#include <cmath>4#include <cstring>5#include <algorithm>6#include <Set>7 using namespacestd;8 Charch;9 BOOLOK;Ten voidReadint&x) { One      for(ok=0, Ch=getchar ();! IsDigit (CH); Ch=getchar ())if(ch=='-') ok=1; A      for(x=0; isdigit (ch); x=x*Ten+ch-'0', ch=GetChar ()); -     if(OK) x=-x; - } the Const intmaxn=100005; - Const intmaxm=200005; - intN,Q,OP,A,B,V,TOT,NOW[MAXN],SON[MAXM],PRE[MAXM],FA[MAXN],COL[MAXN]; - structlct{ +     intid,fa[maxn],son[maxn][2],VAL[MAXN],MAXV[MAXN]; -multiset<int>REST[MAXN]; +     intWhich (intx) {returnson[fa[x]][1]==x;} A     BOOLIsRootintx) {returnson[fa[x]][0]!=x&&son[fa[x]][1]!=x;} at     voidUpdateintx) { -maxv[x]=Val[x]; -         if(!rest[x].empty ()) Maxv[x]=max (maxv[x],*Rest[x].rbegin ()); -         if(son[x][0]) Maxv[x]=max (maxv[x],maxv[son[x][0]]); -         if(son[x][1]) Maxv[x]=max (maxv[x],maxv[son[x][1]]); -     } in     voidRotateintx) { -         intY=fa[x],z=fa[y],d=which (x), dd=which (y); tofa[son[x][d^1]]=y,son[y][d]=son[x][d^1],fa[x]=Fa[y]; +         if(!isroot (y)) son[z][dd]=x; -fa[y]=x,son[x][d^1]=y,update (y), update (x); the     } *     voidSplay (intx) { $          while(!isroot (x)) {Panax Notoginseng             if(IsRoot (fa[x])) rotate (x); -             Else if(which (x) = =which (fa[x]) rotate (fa[x]), rotate (x); the             Elserotate (x), rotate (x); +         } A     } the     voidAccessintx) { +          for(intp=0; x;x=Fa[x]) { - splay (x); $             if(son[x][1]) Rest[x].insert (maxv[son[x][1]]); $             if(P) rest[x].erase (Rest[x].find (maxv[p)); -son[x][1]=p,update (p=x); -         } the     } -     voidLinkintXinty) {Wuyi         if(!y)return; theAccess (y), splay (y), splay (x), fa[x]=y,son[y][1]=x,update (y); -     } Wu     voidCutintXinty) { -         if(!y)return; AboutAccess (x), splay (x), fa[son[x][0]]=0, son[x][0]=0, update (x); $     } -     intFind_root (intx) { for(Access (x), splay (x); son[x][0];x=son[x][0]);returnx;} -     voidQueryintx) { -         intt=find_root (x); splay (t); Aprintf"%d\n", col[t]==id?maxv[t]:maxv[son[t][1]]); +     } the     voidModifyintXintV) {access (x), splay (x), val[x]=v,update (x);} -}t[2]; $ voidPutintAintb) {pre[++tot]=now[a],now[a]=tot,son[tot]=b;} the voidAddintAintb) {Put (A, b), put (b,a);} the voidDfsintu) { the      for(intP=now[u],v=son[p];p; p=pre[p],v=Son[p]) the         if(V!=fa[u]) fa[v]=U,t[col[v]].link (v,u), DFS (v); - } in intMain () { theRead (n), t[0].id=0, t[1].id=1; the      for(intI=1; i<n;i++Read (a), read (b), add (a); About      for(intI=1; i<=n;i++) read (Col[i]); the      for(intI=1; i<=n;i++) Read (t[0].val[i]), t[0].maxv[i]=t[0].val[i]; the      for(intI=1; i<=n;i++) t[1].maxv[i]=t[1].val[i]=t[0].val[i]; theDfs1); +      for(Read (q); q;q--){ - Read (OP), read (a); the         if(op==0) T[col[a]].query (a);Bayi         Else if(op==1) T[col[a]].cut (A,fa[a]), col[a]^=1, T[col[a]].link (A,fa[a]); the         ElseRead (v), t[0].modify (a,v), t[1].modify (a,v); the     } -     return 0; -}

Bzoj3639:query on a tree VII

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.