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
树链剖分入门题了。不懂可以先看这篇博文:http://blog.sina.com.cn/s/blog_7a1746820100wp67.html
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define maxn 100080 #define maxm 200160 #define lson id<<1,l,mid #define rson id<<1|1,mid+1,r int first[maxn],e; int vv[maxm],nxt[maxm],ww[maxm]; int fa[maxn];//父亲节点 int top[maxn];//top[v]表示v所在的重链的顶端节点 int dep[maxn];//深度 int size[maxn];//size[v]表示以v为根的子树的节点数 int p[maxn];//p[v]表示以v与其父亲节点的连边在线段树中的位置 int fp[maxn];//和数组相反 int son[maxn];//重儿子 int pos; //dfs1函数确定fa,dep,size,son void init() { pos = 1; e = 0; memset(first,-1,sizeof(first)); memset(son,-1,sizeof(son)); } void addedge(int u,int v,int w) { vv[e] = v; ww[e] = w; nxt[e] = first[u]; first[u] = e++; vv[e] = u; ww[e] = w; nxt[e] = first[v]; first[v] = e++; } void dfs1(int u,int pre,int d) { fa[u] = pre; dep[u] = d; size[u] = 1; for(int i = first[u];i != -1;i = nxt[i]) { int v = vv[i]; if(v == pre) continue; dfs1(v,u,d+1); size[u] += size[v]; if(son[u] == -1 || size[v] > size[son[u]]) son[u] = v; } } //dfs2确定top,p void dfs2(int u,int t) { top[u] = t; if(son[u] != -1) { p[u] = pos++;//p[u]表示连向u的这条边在线段树中的位置 fp[p[u]] = u; dfs2(son[u],t); } else { p[u] = pos++; fp[p[u]] = u; return; } for(int i = first[u];i != -1;i = nxt[i]) { int v = vv[i]; if(v == son[u] || v == fa[u]) continue; dfs2(v,v); } } struct ST { int l,r,Max; }st[maxn<<2]; void build(int id,int l,int r) { st[id].l = l;st[id].r = r; st[id].Max = 0; if(l == r) return; int mid = (l+r) >> 1; build(lson); build(rson); } void PushUp(int id) { st[id].Max = max(st[id<<1].Max,st[id<<1|1].Max); } void Update(int id,int pos,int k) { if(st[id].l == pos && st[id].r == pos) { st[id].Max = k; return; } if(st[id<<1].r >= pos) Update(id<<1,pos,k); else if(st[id<<1|1].l <= pos) Update(id<<1|1,pos,k); PushUp(id); } int query(int id,int l,int r) { if(st[id].l == l && st[id].r == r) return st[id].Max; int mid = (l+r) >> 1; if(st[id<<1].r >= r) return query(id<<1,l,r); else if(st[id<<1|1].l <= l) return query(id<<1|1,l,r); return max(query(id<<1,l,st[id<<1].r),query(id<<1|1,st[id<<1|1].l,r)); } int find(int u,int v)//找u和v路径上的最大边 { int f1 = top[u],f2 = top[v]; int ans = 0; while(f1 != f2) { if(dep[f1] < dep[f2]) { swap(f1,f2); swap(u,v); } ans = max(ans,query(1,p[f1],p[u])); u = fa[f1]; f1 = top[u]; } if(u == v) return ans; if(dep[u] > dep[v]) swap(u,v); return max(ans,query(1,p[son[u]],p[v])); } int main() { int T; int n; scanf("%d",&T); while(T--) { init(); scanf("%d",&n); for(int i = 0;i < n-1;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); addedge(u,v,w); } dfs1(1,0,0); dfs2(1,1); build(1,1,n); char ope[10]; for(int i = 0;i < n;i++) { for(int j = first[i];j != -1;j = nxt[j]) { int v = vv[j]; if(dep[i] < dep[v]) { Update(1,p[v],ww[j]); } } } while(scanf("%s",ope)!=EOF && ope[0] != 'D') { int u,v; scanf("%d%d",&u,&v); if(ope[0] == 'Q') { printf("%d\n",find(u,v)); } else { int cnt1 = 2*u-1; int cnt2 = 2*u-2; int cnt3 = dep[vv[cnt1]] > dep[vv[cnt2]]?vv[cnt1]:vv[cnt2]; Update(1,p[cnt3],v); } } } return 0; }