Description
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1.
We will 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:
- In the first line there is 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 is signified by the string "DONE".
There is one blank line between successive tests.
Output
For each "QUERY" operation, write one integer representing its result.
Example
Input: 1 3 1 2 1 2 3 2 QUERY 1 2 CHANGE 1 3 QUERY 1 2 DONE Output: 1 3 分析:第一次敲树链剖分,参考了网上的模板。#include<iostream> #include<string> #include<algorithm> #include<cstdlib> #include<cstdio> #include<set> #include<map> #include<vector> #include<cstring> #include<stack> #include<cmath> #include<queue> #define INF 0x3f3f3f3f #define N 100005 using namespace std; int dep[N],siz[N],fa[N],id[N],son[N],val[N],top[N],fid[N]; //深度,儿子数量,父亲,新编号,重儿子,权值,所属链头,反编号。 int T,n,num; vector <int> G[N]; struct Edge { int x,y,val; } edge[N]; struct Segtree { int l,r,val,down; } tree[4*N]; void dfs1(int u,int f,int d) { dep[u] = d; siz[u] = 1; son[u] = 0; fa[u] = f; for(int v : G[u]) if(v != f) { dfs1(v,u,d+1); siz[u] += siz[v]; if(siz[son[u]] < siz[v]) son[u] = v; } } void dfs2(int u,int tp) { top[u] = tp; id[u] = ++num; fid[id[u]] = u; if(son[u]) dfs2(son[u],tp); for(int v : G[u]) if(v != fa[u] && v != son[u]) dfs2(v,v); } void build(int i,int l,int r) { tree[i].l=l; tree[i].r=r; tree[i].down = 0; if(l == r) { tree[i].val = val[fid[l]]; return; } int mid=(l+r)>>1; build(2*i,l,mid); build(2*i+1,mid+1,r); tree[i].val = max(tree[2*i].val,tree[2*i+1].val); } void Insert(int i,int x,int Val) { int l = tree[i].l,r = tree[i].r; if(l == r) { tree[i].val = Val; return; } int mid = (l+r)/2; if(x <= mid) Insert(2*i,x,Val); else Insert(2*i+1,x,Val); tree[i].val = max(tree[2*i].val,tree[2*i+1].val); } int query(int i,int x,int y) { int l = tree[i].l,r = tree[i].r; if(l == x && r == y) { return tree[i].val; } int mid = (l+r)/2; if(y <= mid) return query(2*i,x,y); else if(x <= mid) return max(query(2*i,x,mid),query(2*i+1,mid+1,y)); else return query(2*i+1,x,y); } int Yougth(int u,int v) { int tp1 = top[u],tp2 = top[v]; int ans = 0; while(tp1 != tp2) { if(dep[tp1] < dep[tp2]) { swap(tp1,tp2); swap(u,v); } ans = max(query(1,id[tp1],id[u]),ans); u = fa[tp1]; tp1 = top[u]; } if(u == v) return ans; if(dep[u] > dep[v]) swap(u,v); ans = max(query(1,id[son[u]],id[v]),ans); return ans; } int main() { scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i = 1;i <= n;i++) G[i].clear(); for(int i = 1;i < n;i++) { scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].val); G[edge[i].x].push_back(edge[i].y); G[edge[i].y].push_back(edge[i].x); } num = 0; dfs1(1,0,1); dfs2(1,1); for(int i = 1;i < n;i++) if(dep[edge[i].x] > dep[edge[i].y]) val[edge[i].x] = edge[i].val; else val[edge[i].y] = edge[i].val; build(1,1,num); char s[20]; while(~scanf("%s",s) && s[0] != 'D') { int x,y; scanf("%d%d",&x,&y); if(s[0] == 'Q') printf("%d\n",Yougth(x,y)); else { if(dep[edge[x].x] > dep[edge[x].y]) Insert(1,id[edge[x].x],y); else Insert(1,id[edge[x].y],y); } } } }