Get started see this article http://blog.sina.com.cn/s/blog_7a1746820100wp67.html
An introductory topic
qtree-query on a tree
no tags
You is 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 in 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 an 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
Code:
#include <cstdio>#include<cstring>#include<iostream>#defineLson L, M, rt<<1#defineRson m+1, R, rt<<1|1using namespacestd;Const intMAXN =10010;structnode{intto, Next;};intP[MAXN];//position in the first segment tree, DFS sequenceinttree[maxn*4];//Segment TreeintFA[MAXN];//Save the parent node of the current nodeintTOP[MAXN];//node that represents the top of the chain at which the current node is locatedintSIZ[MAXN];//the number of all child nodes of the current nodeintDEEP[MAXN];//depth of current nodeintHEAD[MAXN];//forward-to-star representationintSON[MAXN];//the heavy son of the current nodeNode edge[maxn*2];//notation of the forward starinte[maxn][3];//Edge SetintTot, POS;//tot represents the number of all nodes in the Forward star, and Pos represents the position in the segment treeintMax (intAintb) { returna > B?a:b;}voidinit () {tot=0;//initialized to 0pos =1;//because I wrote the line segment Tree 1 is the root node, so this starts with 1Memset (Head,-1,sizeof(head)); memset (son,-1,sizeof(son)); }//Adding edgesvoidAddedge (intUintv) {edge[tot].to=v; Edge[tot].next=Head[u]; Head[u]= tot++;}//first Dfs, find out how many children the current node has, its father, the depth, and the heavy sonvoidDFS1 (intUintPreintd) {Siz[u]=1; Fa[u]=Pre; Deep[u]=D; for(inti = Head[u]; I! =-1; i = edge[i].next)//Traverse all edges { intv =edge[i].to; if(v! = Pre)//can't go up because this is a no-show diagram{DFS1 (V, u, D+1); Siz[u]+=Siz[v]; if(Son[u] = =-1|| Siz[son[u]] <Siz[v]) Son[u]=v; } }}//the second DFS asks for top, and PvoidDFS2 (intUintsp) {Top[u]=sp; if(Son[u]! =-1)//not a leaf vertex .{P[u]= pos++; DFS2 (Son[u], SP); } Else//Leaf Apex{P[u]= pos++; return; } for(inti = Head[u]; I! =-1; i = edge[i].next)//find all the vertices of its connection { intv =edge[i].to; if(Son[u]! = v && v! = Fa[u])//not a heavy son and can't look up .{dfs2 (V, v); } } }//Segment TreevoidPushup (intRT) {Tree[rt]= Max (tree[rt<<1], tree[rt<<1|1]);}voidBuildintLintRintRT) { if(L = =R) {Tree[rt]=0; return; } intm = (L + r) >>1; Build (Lson); Build (Rson); Pushup (RT); }voidUpdateintPintScintLintRintRT) { if(L = =R) {Tree[rt]=SC; return; } intm = (L + r) >>1; if(P <=m) Update (p, SC, Lson); ElseUpdate (P, SC, Rson); Pushup (RT); }intQueryintLintRintLintRintRT) { if(l <= l && R <=R) {returnTree[rt]; } intm = (L + r) >>1; intres =0; if(L <=m) Res=Max (res, query (L, R, Lson)); if(R >m) Res=Max (res, query (L, R, Rson)); returnRes;}//Query The maximum value of the u->v edgeintFindintUintv) { intF1 = Top[u], F2 =Top[v]; intAns =0; while(F1! =F2) { if(Deep[f1] <Deep[f2]) {Swap (f1, F2); Swap (U, v); } ans= Max (ans, query (P[F1], P[u],1, POS-1,1)); U=FA[F1]; F1=Top[u]; } if(U = =v)returnans; if(Deep[u] >Deep[v]) Swap (U, v); returnMax (ans, query (P[son[u]], p[v],1, POS-1,1)); }intMain () {//freopen ("In.txt", "R", stdin); intT, N; scanf ("%d", &T); while(t--) {init (); scanf ("%d", &N); for(inti =0; I < n-1; i++) {scanf (" %d%d%d", &e[i][0], &e[i][1], &e[i][2]); Addedge (e[i][0], e[i][1]);//No direction graph, so add two timesAddedge (e[i][1], e[i][0]); } DFS1 (1,0,0); DFS2 (1,1); Build (1, POS-1,1); for(inti =0; I < n-1; i++) { if(deep[e[i][0]] > deep[e[i][1]]) swap (e[i][0], e[i][1]); Update (p[e[i][1]], e[i][2],1, POS-1,1);//update weights to segment tree } Charop[Ten]; intu, v; while(~SCANF ("%s", op)) { if(op[0] =='D') Break; Else if(op[0] =='C') {scanf ("%d%d", &u, &v); Update (P[e[u-1][1]], V,1, POS-1,1); } Else{scanf ("%d%d", &u, &v); intt =Find (U, v); printf ("%d\n", T); } } } return 0;}
Tree Chain Split