Click to open link
Test instructions: Give a tree and tree weight, two operations, Q U V, ask u to V on the edge of the maximum weight, C u V, the weight of the first side of the value of the change to V
Idea: Today learned to learn the tree chain, the problem is a test template of the topic, the theory I was not clear, my own in nine wild poly that learned a template
#include <vector> #include <stdio.h> #include <string.h> #include <stdlib.h> #include < Iostream> #include <algorithm>using namespace std;typedef long long ll;typedef unsigned long long ull;const int in f=0x3f3f3f3f;const ll Inf=0x3f3f3f3f3f3f3f3fll;const int Maxn=10010;int FA[MAXN],SIZ[MAXN],SON[MAXN],W[MAXN],P[MAXN ],DEP[MAXN],FP[MAXN];//FA is the parent node, Siz is the largest siz in the child node, DEP is the depth, son is heavy, and W represents the position in the segment tree, int max1[maxn<<2];int tree_id,n; Vector<int>g[maxn];void dfs1 (int u,int ff,int deep) {son[u]=0;fa[u]=ff;siz[u]=1;dep[u]=deep; for (unsigned int i=0;i<g[u].size (); i++) {int v=g[u][i]; if (V==FF) continue; DFS1 (v,u,deep+1); SIZ[U]+=SIZ[V]; if (Siz[v]>siz[son[u]]) son[u]=v; }}void DFS2 (int u,int ff) {w[u]=++tree_id;p[u]=ff; if (Son[u]) DFS2 (SON[U],FF); else return; for (unsigned int i=0;i<g[u].size (); i++) {int v=g[u][i]; if (V!=fa[u]&&v!=son[u]) DFS2 (V,V); }}void Update (int pos,int VAL,int le,int ri,int node) {if (Le==ri) {max1[node]=val; return; } int t= (LE+RI) >>1; if (pos<=t) update (POS,VAL,LE,T,NODE<<1); else update (POS,VAL,T+1,RI,NODE<<1|1); Max1[node]=max (max1[node<<1],max1[node<<1|1]);} int query (int l,int r,int le,int ri,int node) {if (l<=le&&ri<=r) return Max1[node]; int t= (LE+RI) >>1,ans=0; if (l<=t) Ans=max (Ans,query (l,r,le,t,node<<1)); if (r>t) Ans=max (Ans,query (l,r,t+1,ri,node<<1|1)); return ans;} int Getans (int u,int v) {int f1=p[u],f2=p[v],tmp=0; while (F1!=F2) {if (Dep[f1]<dep[f2]) {swap (F1,F2); Swap (U,V); } Tmp=max (Tmp,query (w[f1],w[u],1,n,1)); U=fa[f1];f1=p[u]; } if (U==V) return tmp; if (Dep[u]>dep[v]) swap (U,V); Return Max (Tmp,query (w[son[u]],w[v],1,n,1));} int U[maxn],v[maxn],c[maxn];int Main () {int t,u,v; Char str[10]; scanf ("%d", &t); while (t--) { scanf ("%d", &n); for (int i=0;i<maxn;i++) g[i].clear (); memset (son,0,sizeof (son)); tree_id=0; for (int i=0;i<n-1;i++) {scanf ("%d%d%d", &u[i],&v[i],&c[i]); G[u[i]].push_back (V[i]); G[v[i]].push_back (U[i]); } DFS1 (1,1,0); DFS2 (a); memset (max1,0,sizeof (MAX1)); for (int i=0;i<n-1;i++) {if (Dep[u[i]]>dep[v[i]) swap (u[i],v[i]); Update (w[v[i]],c[i],1,n,1); } while (1) {scanf ("%s", str); if (str[0]== ' D ') break; scanf ("%d%d", &u,&v); if (str[0]== ' C ') update (w[v[u-1]],v,1,n,1); else printf ("%d\n", Getans (u,v)); }} return 0;}
SPOJ 375 Tree Chain split