"Topic link" http://poj.org/problem?id=2763
"The main topic"
In a tree, give some sides of the edge length, there are modifications to the side of the side of the long operation,
Ask for the shortest distance from the current point to the target point each time
Exercises
After the tree chain is split, it is equivalent to a single point update and interval query of the tree array,
Note that the chain operation does not cover the deep shallow point after the edge right turning point, error prone here
Code
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int n=200010; int Tot,x,d[n],num[n],ed=0,u,w,n,m,i,v[n],vis[n],f[n],g[n];int nxt[n],size[n],son[n],st[n],en[n],dfn,top[n],t; Char ch;void add_edge (int x,int y) {v[++ed]=y;nxt[ed]=g[x];g[x]=ed;} void Init () {memset (g,dfn=ed=0,sizeof (g)); memset (v,0,sizeof (v)); memset (nxt,0,sizeof (NXT)); memset (son,-1,sizeof (son)); void Dfs (int x) {size[x]=1; for (int i=g[x];i;i=nxt[i]) if (V[i]!=f[x]) {f[v[i]]=x,d[v[i]]=d[x]+1; DFS (V[i]), Size[x]+=size[v[i]]; if (Size[v[i]]>size[son[x]]) son[x]=v[i]; }}void dfs2 (int x,int y) {if (x==-1) return; St[x]=++dfn;top[x]=y; if (Son[x]) DFS2 (son[x],y); for (int i=g[x];i;i=nxt[i]) if (v[i]!=son[x]&&v[i]!=f[x]) DFS2 (V[i],v[i]); EN[X]=DFN;} int c[n];void Add (int x,int val) {while (X<=DFN) c[x]+=val,x+=x&-x;} int query (int x) {int s=0;while (x) S+=c[x],x-=x&-x;return s;} int chain (int x,int y) {int res=0; For(Top[x]!=top[y];x=f[top[x]]) {if (D[top[x]]<d[top[y]]) {int z=x;x=y;y=z;} Res=res+query (St[x])-query (st[top[x]]-1); }if (D[x]<d[y]) {int z=x;x=y;y=z;} Note that the edge of the right to sub-node point, the chain operation does not operate deep the most shallow point, the left end is open node Res=res+query (st[x])-query (St[y]); return res; }int E[n][3],q,s;int Main () {while (~scanf ("%d%d%d", &n,&q,&s)) {init (); Memset (C,0,sizeof (c)); for (int i=1;i<n;i++) {scanf ("%d%d%d", &e[i][0],&e[i][1],&e[i][2]); Add_edge (e[i][0],e[i][1]); Add_edge (E[i][1],e[i][0]); }dfs (1);d fs2 (); for (int i=1;i<n;i++) {if (d[e[i][0]]>d[e[i][1]) swap (e[i][0],e[i][1]); Add (st[e[i][1]],e[i][2]); }int Op,x,y; while (q--) {scanf ("%d", &op); if (op==0) {scanf ("%d", &x); printf ("%d\n", Chain (x,s)); S=x; }else{scanf ("%d%d", &x,&y); Add (St[e[x][1]],Y-E[X][2]); E[x][2]=y; }}}return 0;}
POJ 2763 Housewife Wind (tree-chain + tree-shaped array)