Topic links
To a tree, each edge has the right value, two operations, one is to change the weight of an edge, one is to ask you to V path on the largest edge of the weight value.
Tree-chain split template.
#include <iostream>#include<vector>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<map>#include<Set>#include<string>#include<queue>#include<stack>#include<bitset>using namespacestd;#definePB (x) push_back (x)#definell Long Long#defineMK (x, y) make_pair (x, y)#defineLson L, M, rt<<1#defineMem (a) memset (a, 0, sizeof (a))#defineRson m+1, R, rt<<1|1#defineMem1 (a) memset (a,-1, sizeof (a))#defineMEM2 (a) memset (a, 0x3f, sizeof (a))#defineRep (i, N, a) for (int i = A; i<n; i++)#defineFi first#defineSe Secondtypedef pair<int,int>PLL;Const DoublePI = ACOs (-1.0);Const DoubleEPS = 1e-8;Const intMoD = 1e9+7;Const intINF =1061109567;Const intdir[][2] = { {-1,0}, {1,0}, {0, -1}, {0,1} };Const intMAXN =10005;inthead[maxn*2], SON[MAXN], SZ[MAXN], DEEP[MAXN], TOP[MAXN], W[MAXN], F[MAXN], CNT, num;intedge[maxn][3], maxx[maxn<<2];structnode{intto, Nextt;} E[MAXN*2];voidinit () {mem1 (head); Num= CNT =0;}voidAddintUintVintW) {e[num].to= V, e[num].nextt = Head[u], head[u] = num++;}voidDFS1 (intUintFA) {Sz[u]=1; Deep[u]= deep[fa]+1; Son[u]= -1; F[u]=FA; for(inti = Head[u]; ~i; i =e[i].nextt) { intv =e[i].to; if(v = =FA)Continue; DFS1 (V, u); Sz[u]+=Sz[v]; if(son[u]==-1|| Sz[v]>Sz[son[u]]) Son[u]=v; }}voidDFS2 (intUintTP) {W[u]= ++cnt, Top[u] =TP; if(~Son[u]) DFS2 (Son[u], TP); for(inti = Head[u]; ~i; i =e[i].nextt) { intv =e[i].to; if(v = = f[u]| | v = =Son[u])Continue; DFS2 (V, v); }}voidPushup (intRT) {Maxx[rt]= Max (maxx[rt<<1], maxx[rt<<1|1]);}voidUpdateintPintValintLintRintRT) { if(L = =R) {Maxx[rt]=Val; return ; } intm = l+r>>1; if(p<=m) Update (p, Val, Lson); ElseUpdate (P, Val, Rson); Pushup (RT);}intQueryintLintRintLintRintRT) { if(l<=l&&r>=r) {returnMaxx[rt]; } intm = l+r>>1, ret =0; if(l<=m) Ret=Max (ret, query (L, R, Lson)); if(r>m) Ret=Max (ret, query (L, R, Rson)); returnret;}intFindintUintv) {intF1 = Top[u], F2 = top[v], ret =0; while(F1! =F2) { if(deep[f1]<Deep[f2]) {Swap (f1, F2); Swap (U, v); } ret= MAX (ret, query (W[F1], W[u],1Cnt1)); U=F[F1]; F1=Top[u]; } if(U = =v)returnret; if(deep[u]>Deep[v]) Swap (U, v); returnMax (ret, query (W[son[u]], w[v],1Cnt1));}intMain () {intT, N, U, V, Val; CIN>>T; while(t--) {scanf ("%d", &N); Init (); deep[0] =0; for(inti =1; i<n; i++) {scanf ("%d%d%d", &u, &v, &val); Add (U, V, Val); Add (V, U, Val); edge[i][0] = u, edge[i][1] = V, edge[i][2] =Val; } DFS1 (1,0); DFS2 (1,1); for(inti =1; i<n; i++) { if(deep[edge[i][0]]>deep[edge[i][1]]) {Swap (edge[i][0], edge[i][1]); } Update (w[edge[i][1]], edge[i][2],1Cnt1); } Chars[Ten]; while(SCANF ("%s", s)) { if(s[0] =='D') Break; intx, y; scanf ("%d%d", &x, &y); if(s[0]=='Q') {printf ("%d\n", find (x, y)); } Else{update (w[edge[x][1], Y,1Cnt1); } } } return 0;}
Spoj 375 Qtree-query on a tree chain split