"LCT" poj2763 housewife Wind

Source: Internet
Author: User

Test instructions: Give you a tree, with right side, support two actions: Modify the weight of an edge, and query the shortest path between two points.

LCT mainly implements single point modification and path and.

Modifying the value of an X-node simply splay the x to the root of its secondary tree, then modifies its value, and then maintain it.

Path and ask to do this:

We first access (U), and then in Access (v), once we get to the auxiliary Tree that contains the root node (that is, the U), then we go to the nearest common ancestor of U and V, so we can call this node D. The maxcost of the right subtree of D in the auxiliary tree is the maximum edge of the path from U to D, and the maxcost of the auxiliary tree where v resides is the maximum edge of the path of V to D. So the answer we're asking for is the maximum value in the 6 of these two maxcost. Because the averaging complexity of an Access operation is O (log n), the time it takes to answer this query is also O (log n).

#include <cstdio> #include <iostream> #include <cstring> #include <algorithm>using namespace STD; #define MAXN 100005int Fa[maxn],c[maxn][2],siz[maxn];bool is_root[maxn];int val[maxn],totalval[maxn];void    Maintain (int x) {siz[x]=siz[c[x][0]]+siz[c[x][1]]+1; TOTALVAL[X]=TOTALVAL[C[X][0]]+TOTALVAL[C[X][1]]+VAL[X];}    void Rotate (int x,bool flag) {int y=fa[x];    C[y][!flag]=c[x][flag];    if (C[x][flag]) {fa[c[x][flag]]=y;    } if (Fa[y] && c[fa[y]][c[fa[y]][1]==y]==y) {c[fa[y]][c[fa[y]][1]==y]=x;    } Fa[x]=fa[y];    C[x][flag]=y;    Fa[y]=x;    if (Is_root[y]) {is_root[y]=0;    Is_root[x]=1; }maintain (y);}    void splay (int x) {if (!x | | is_root[x]) {return;    } int y;        while (Y=fa[x], (!is_root[x])) {if (Is_root[y]) {Rotate (x,c[y][0]==x);            } else{if ((c[y][0]==x) = = (c[fa[y]][0]==y)) {Rotate (y,c[fa[y]][0]==y); } else{Rotate (x,c[y][0]==x);                Y=FA[X];        } Rotate (X,c[y][0]==x); }}maintain (x);}    void Spla2 (int x) {if (!x | | is_root[fa[x]]) {return;    } int y;        while (!is_root[y=fa[x]]) {if (Is_root[fa[y]]) {Rotate (x,c[y][0]==x);            } else{if ((c[y][0]==x) = = (c[fa[y]][0]==y)) {Rotate (y,c[fa[y]][0]==y);                } else{Rotate (x,c[y][0]==x);            Y=FA[X];        } Rotate (X,c[y][0]==x); }} maintain (x);} void Access (int x) {int y; Splay (x); while (Fa[x]) {y=fa[x]; Splay (y); if (C[y][1]) {is_root[c[y][1]]=1;} Is_root[x]=0;c[y][1]=x; Splay (x);} if (C[x][1]) {is_root[c[x][1]]=1;c[x][1]=0;}} int Calc (int x,int y) {if (x==y) {return 0;} Splay (y), if (!fa[y]) {SPLA2 (x); return totalval[c[x][0]]+val[x];} int Z;while (Fa[y]) {z=fa[y]; Splay (z), if (!fa[z]) {return totalval[c[z][1]]+totalval[y];} if (C[z][1]) {is_root[c[z][1]]=1;} Is_root[y]=0;c[z][1]=y; Splay (y);}} int N,m,cur;int V[maxn<<1],Nex[maxn<<1],first[maxn],w[maxn<<1],e;void addedge (int u,int v,int W) {v[++e]=v;w[e]=w;nex[e]=first[u]; First[u]=e;} int Bel[maxn];bool vis[maxn];void DFS (int U) {vis[u]=1;for (int i=first[u];i;i=nex[i]) {if (!vis[v[i]]) {bel[i+1>> 1]=v[i];siz[v[i]]=1;is_root[v[i]]=1;val[v[i]]=totalval[v[i]]=w[i];fa[v[i]]=u;dfs (V[i]);}} int main () {int Op,x,y,z;//freopen ("poj2763.in", "R", stdin),//freopen ("Poj2763.out", "w", stdout), scanf ("%d%d%d", &n,&m,&cur); for (int i=1;i<n;++i) {scanf ("%d%d%d", &x,&y,&z); Addedge (x, y, z); Addedge (y,x,z);} Siz[1]=1;is_root[1]=1;dfs (1); for (int i=1;i<=m;++i) {scanf ("%d%d", &op,&x), if (!op) {Access (x);p rintf ("%d \ n ", Calc (X,cur)); cur=x;} ELSE{SCANF ("%d", &y); Splay (Bel[x]); val[bel[x]]=y;    Maintain (bel[x]);}} return 0;}

"LCT" poj2763 housewife Wind

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.