Hdoj topic 2763 Housewife Wind (Link Cut tree Modify edge, query distance between two points)

Source: Internet
Author: User

Housewife Wind
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 7639 Accepted: 1990

Description

After their royal wedding, Jiajia and wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beautiful huts. There is some pairs of huts connected by bidirectional roads. We say that huts in the same pair directly connected. XX Village are so special, we can reach any and huts starting from an arbitrary hut. If Each road cannot was walked along twice, then the route between every pair is unique.

Since Jiajia earned enough, Wind became a housewife. Their children loved to go to other kids, then make a simple call to wind: ' Mummy, take Me home! '

At different times, the time needed to walk along a road could be different. For example, Wind takes 5 minutes on a road normally, but could take the minutes if there is a lovely little dog to play with , or take 3 minutes if there is some unknown strange smell surrounding the road.

Wind loves his children, so she would like to tell her children the exact time she'll spend on the roads. Can you help her?

Input

The first line contains three integers n, q, S. There was n huts in XX Village, q messages to process, and wind was currently in Hut S. N < 100001, q < 100001.

The following n-1 lines each contains three integers a, B and W. That means there was a road directly connecting hut A and B, time required is W. 1<=w<= 10000.

The following q lines is one of the following and the types:

Message a:0 u
A kid in Hut U calls wind. She should go to hut u from hers current position.
Message B:1 i W
The time required for I-th Road was changed to W. Note that the time change would not be happen when wind was on her road. The changed can only happen if wind was staying somewhere, waiting to take the next kid.

Output

For each message A, print an integer X, the time required to take the next child.

Sample Input

3 3 11 2 12 3 20 21 2 30 3

Sample Output

13

Source

POJ Monthly--2006.02.26,zgl & TWB

The main idea: Operation 0 Query The current point to this point of the shortest distance, operation 1 Modify the weights of the K-side

problem:2763user:kxh1995memory:7636ktime:1141mslanguage:c++result:accepted


AC Code

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <queue> #include <  Iostream> using namespace std;  int head[100010],cnt,vis[100050];  struct S {int u,v,w,next;  }edge[150050<<1];      struct LCT {int bef[150050],pre[150050],next[150050][2],key[150050],sum[150050],belong[150050];          void Init () {memset (pre,0,sizeof (pre));        memset (Next,0,sizeof (next));      } void Pushup (int x) {sum[x]=key[x]+sum[next[x][1]]+sum[next[x][0]];            } void rotate (int x,int kind) {int y,z;            Y=PRE[X];            Z=pre[y];            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; while (Pre[x]) {if (next[pre[x]][0]==x) {Rotat                    E (x,1);                } else rotate (x,0);          } pushup (x);            }} void access (int x) {int fa;                for (Fa=0;x;x=bef[x]) {splay (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);          }} void Change (int x,int val) {int t;          T=BELONG[X-1];          Key[t]=val;      Splay (t);          } int query (int x,int y) {access (Y);              for (Y=0;x;x=bef[x]) {splay (x);    if (!bef[x]) return sum[y]+sum[next[x][1]];          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;void Add (int u,int v,int W) {edge[cnt].u=u;      Edge[cnt].v=v;      Edge[cnt].w=w;      Edge[cnt].next=head[u];  head[u]=cnt++;      } void BFs (int u) {int i,y;      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;                  LCT.KEY[V]=LCT.SUM[V]=EDGE[I].W;                  lct.belong[i>>1]=v;                  Vis[v]=1;              Q.push (v); }}}} int main () {int n,m,s;while (scanf ("%d%d%d", &n,&m,&s)!=eof) {int i;lct.init (); Cnt=0;memse T (head,-1,sizeof (head)); for (i=1;i<n;i++) {int u,v,w;scanf ("%d%d%d", &u,&v,&w); add (u,v,w); add (v,u,w);} BFS (1), while (m--) {int op;scanf ("%d", &op), if (!op) {int x;scanf ("%d", &x);p rintf ("%d\n", Lct.query (s,x)); s=x;} Else{int x,y;scanf ("%d%d", &x,&y); Lct.change (x, y);}}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdoj topic 2763 Housewife Wind (Link Cut tree Modify edge, query distance between two points)

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.