Bzoj 3639:query on a tree VII

Source: Internet
Author: User

Description

A tree that supports three operations, modifies the point right, modifies the color, and asks all the same points with the same color on his path, including these two points.

Sol

Lct.

With LCT to maintain the heavy edge, for each node under construction a set to maintain the light edge, so link and cut is very good operation, direct access, splay, directly delete it.

Because set is not counted for heavy edges, then the information for each node is saved by his father, because a node may have many sons but must have only one father.

Another problem is that the weights of each point cannot be built globally, because the two LCT that are maintained cannot be deleted at the same time, so each LCT has a point-weighted array.

Code
/************************************************************** problem:3639 User:beiyu language:c++ Result : Accepted time:4060 ms memory:16276 kb****************************************************************/#include &L T;bits/stdc++.h>using namespace Std; #define DEBUG (a) cout<< #a << "=" <<a<< "typedef long LONG ll;const int N = 1e5+50;    inline int in (int x=0,char Ch=getchar (), int. v=1) {while (ch> ' 9 ' | | ch< ' 0 ') v=ch== '-'? -1:v,ch=getchar (); while (ch>= ' 0 ' && ch<= ' 9 ') x=x*10+ch-' 0 ', Ch=getchar (); return x*v; } int N,q;vector < int > G[n];int col[n],f[n];    struct Linkcuttree {int f[n],ch[n][2];    int mx[n],w[n];         multiset< int,greater< int > > S[n]; #define LC (O) ch[o][0] #define RC (o) ch[o][1] int isrt (int o) {return f[o]==0 | | (LC (F[o])!=o && RC (F[o])!=o);        } void Update (int o) {mx[o]=w[o];        if (!s[o].empty ()) Mx[o]=max (Mx[o],*s[o].begin ()); IF (LC (O)) Mx[o]=max (MX[O],MX[LC (o)));    if (RC (o)) Mx[o]=max (MX[O],MX[RC (o)));        } void Rot (int o) {int p=f[o],k=f[p],r=rc (p) ==o;        if (!ISRT (p)) CH[K][RC (k) ==p]=o;        F[ch[o][r^1]]=p,f[p]=o,f[o]=k;        Ch[p][r]=ch[o][r^1],ch[o][r^1]=p;    Update (P), update (o);        } void splay (int o) {//cout<< "S" <<endl; for (;! ISRT (o);)            {int p=f[o],k=f[p];             if (ISRT (p)) Rot (o);            else if (RC (p) ==o) = = (RC (k) ==p)) Rot (p), rot (o);        else Rot (o), rot (o);    }update (o);        } void Access (int o) {///cout<< "A" <<endl;            for (int p=0;o;p=o,o=f[o]) {splay (o);            if (RC (o)) S[o].insert (MX[RC (o)));        if (RC (o) =p,p) s[o].erase (S[o].find (mx[p]));        }} void Link (int o) {//cout<< "L" <<endl;        Access (F[o]), splay (F[o]), splay (o);    F[O]=F[O],RC (F[o]) =o;        } void Cut (int o) {//cout<< "C" <<endl; Access (o), splay (o), F[LC (o)]=0,lc (o) = 0;        } void Change (int o,int v) {//cout<< "M" <<endl;    Access (o), splay (O), W[o]=v,update (o);        } int Query (int o) {//cout<< "Q" <<endl;        Access (o), splay (o);        int x=o; for (; LC (x); X=LC (x));        Splay (x);        if (Col[x]!=col[o]) return MX[RC (x)];    else return mx[x];  }}PY[2]; void Addedge (int u,int v) {g[u].push_back (v);} void DFS (int u,int fa) {for (int i=0,v;i< (int) g[u].size (); i++) if ((V=g[u][i])!=fa) {F[v]=u,py[col[v]].f[v]=u        ;        DFS (v,u);//debug (Py[col[v]].mx[v]) <<endl;    Py[col[v]].s[u].insert (Py[col[v]].mx[v]); }py[0]. Update (U), py[1]. Update (u);}    void Init () {n=in ();    for (int i=1,u,v;i<n;i++) {u=in (), V=in (), Addedge (u,v), Addedge (V,u);    } for (int i=1;i<=n;i++) col[i]=in ();    for (int i=1;i<=n;i++) py[0].mx[i]=py[0].w[i]=py[1].mx[i]=py[1].w[i]=in (); DFS (+//debug (py[1].mx[1]) <<endl;} int main () {//Freopen ("In.in", "R", sTdin);    Init (); for (int q=in (); q--;)        {int opt=in (), U=in (), V; if (opt==0) printf ("%d\n", Py[col[u]].        Query (u)); else if (opt==1) {if (F[u]) Py[col[u]].            Cut (U);            Col[u]^=1; if (F[u]) Py[col[u]].        Link (U);            } else {v=in (); Py[0].            Change (U,V); PY[1].        Change (U,V); }} return 0;}

  

Bzoj 3639: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.