Description
You were given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3 ... N-1.
We'll ask you to perfrom some instructions of the following form:
- Change i ti : Change the cost of the i-th edge to Ti
Or
- QUERY a B : Ask for the maximum edge cost on the path from Node A to Node B
Input
The first line of input contains an integer T, the number of test cases (T <= 20). T test cases follow.
For each test case:
- The first line there are an integer n (n <= 10000),
- In the next N-1 lines, the i-th line describes the i-th edge:a line with three integers a b c denotes a N Edge between a, b of cost C (C<= 1000000),
- The next lines contain instructions "Change i ti" or "QUERY a B",
- The end of each test case was signified by the string "done".
There is one blank line between successive tests.
Output
For each "QUERY" operation, the write one integer representing its result.
Example
Input:131 2 3 2QUERY 1 2CHANGE 1 3QUERY 1 2DONEOutput:13
should have been write the tree chain, but since you want to practice LCT knock ... It's been a long time.
#include <cstdio>#include<cstring>#include<algorithm>#defineMN 10010using namespacestd;intP,ca,f;inlineintRead () {P=0; Ca=getchar (); f=1; while(ca<'0'|| Ca>'9') {if(ca=='-') f=-1; ca=GetChar ();} while(ca>='0'&&ca<='9') p=p*Ten+ca- -, ca=GetChar (); returnp*F;}structna{intY,ne,c,nu;} B[mn*2];intfa[mn],n,t,x,y,c,l[mn],r[mn],num,id[mn],key[mn],ch[mn][2],MA[MN];BOOLRt[mn];inlineintMaxintAintb) {returnA>b?a:b;} InlinevoidUpdateintx) {Ma[x]=max (Max (ma[ch[x][0]],ma[ch[x][1] ]), key[x]);} InlinevoidRotintx) { inty=fa[x],kind=ch[y][1]==x; FA[X]=Fa[y]; Fa[y]=x; Ch[y][kind]=ch[x][!Kind]; Fa[ch[y][kind]]=y; ch[x][!kind]=y; if(Rt[y]) rt[y]=0, rt[x]=1;Elsech[fa[x]][ch[fa[x]][1]==y]=x; Update (y); update (x);} InlinevoidSplay (intx) { while(!Rt[x]) { if(Rt[fa[x]]) rot (x);Else if((ch[fa[fa[x]]][1]==FA[X]) = = (ch[fa[x]][1]==x)) Rot (fa[x]), rot (x);Elserot (x), rot (x); }}inlinevoidAccintu) { intx=0; while(U) {splay (U); rt[ch[u][1]]=1; rt[ch[u][1]=x]=0; Update (U); U=fa[x=u]; }}inlinevoidChangeintXintc) {ACC (x); KEY[X]=C; Update (x);} InlinevoidLcaint&u,int&v) {ACC (v); v=0; while(U) {splay (U); if(!fa[u]) Break; rt[ch[u][1]]=1; rt[ch[u][1]=v]=0; Update (U); U=fa[v=u]; }}inlineintQuintXinty) {LCA (x, y); returnMax (ma[y],ma[ch[x][1]]);} Inlinevoid inch(intXintYintCintnu) {num++; if(!l[x]) l[x]=num;ElseB[r[x]].ne=num; B[num].y=y;b[num].c=c;b[num].ne=0; b[num].nu=nu;r[x]=num;} InlinevoidDfsintx) { for(intI=l[x];i;i=b[i].ne)if(!Fa[b[i].y]) {FA[B[I].Y]=x; ID[B[I].NU]=b[i].y; KEY[B[I].Y]=b[i].c; DFS (B[I].Y); }}Charss[Ten];intMain () {T=read (); ma[0]=-1e9; while(t--) {num=0; memset (RT,1,sizeof(RT)); memset (FA,0,sizeof(FA)); memset (CH,0,sizeof(CH)); memset (L,0,sizeof(l)); N=read (); for(intI=1; i<n;i++) {x=read (); Y=read (); c=read (); inch(X,y,c,i); inch(Y,x,c,i); } fa[1]=-1; DFS (1); fa[1]=0; for(;;) {scanf ("%s", SS); if(ss[0]=='D') Break; X=read (); y=read (); if(ss[0]=='Q') printf ("%d\n", Qu (x, y));ElseChange (id[x],y); } }}
Spoj qtree-query on a tree