题目链接:http://www.spoj.com/problems/QTREE/en/
Query on a tree
You are given a tree (an acyclic undirected connected graph) withN 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 integersa b c denotes an edge between a, b of costc (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 思路:一道树链剖分的模板题。此题是基于边权的访问,入门级别,故不多讲。详见代码。 附上AC代码:#include <bits/stdc++.h> #define lrt rt<<1 #define rrt rt<<1|1 #define lson l, m, lrt #define rson m+1, r, rrt using namespace std; const int maxn = 10005; // 分别表示以当前节点作为根的子树的节点数目, // 树上各个节点的初始值,当前节点的重儿子 int sizev[maxn], num[maxn], son[maxn]; // 分别表示树链上深度最小的节点,当前节点的深度, // 原节点在剖分后的时间戳,即新的编号 int top[maxn], deep[maxn], pos[maxn]; // 分别表示当前时间戳对应的原节点编号, // 当前节点的父节点 int level[maxn], p[maxn]; // 判断该节点是否被访问过了 bool vis[maxn]; // 分别表示节点数,询问数和时间戳计数 int n, q, cnt; // 存储树的各个节点所连接的边 vector<int> edge[maxn]; void init(){ for (int i=1; i<=n; ++i){ sizev[i] = top[i] = son[i] = 0; deep[i] = pos[i] = level[i] = 0; p[i]=0, vis[i]=false; cnt = 0; edge[i].clear(); } } void add_edge(int u, int v){ edge[u].push_back(v); edge[v].push_back(u); } void dfs1(int u, int root){ vis[u] = true; sizev[u] = 1; p[u] = root; deep[u] = deep[root]+1; int siz = edge[u].size(); for (int i=0; i<siz; ++i){ int v = edge[u][i]; if (v!=p[u] && !vis[v]){ dfs1(v, u); sizev[u] += sizev[v]; if (son[u] == 0) son[u] = v; else if (sizev[son[u]] < sizev[v]) son[u] = v; } } } void dfs2(int u, int root){ vis[u] = true; pos[u] = ++cnt; level[cnt] = u; top[u] = root; if (son[u]) dfs2(son[u], root); int siz = edge[u].size(); for (int i=0; i<siz; ++i){ int v = edge[u][i]; if (v!=p[u] && v!=son[u] && !vis[v]) dfs2(v, v); } } struct node{ int u, v, c; } mess[maxn]; int maxv[maxn<<2]; char op[10]; void push_up(int rt){ maxv[rt] = max(maxv[lrt], maxv[rrt]); } void build(int l, int r, int rt){ maxv[rt] = 0; if (l == r) return ; int m = (l+r)>>1; build(lson); build(rson); } void modify(int p, int val, int l, int r, int rt){ if (l == r){ maxv[rt] = val; return ; } int m = (l+r)>>1; if (p <= m) modify(p, val, lson); else modify(p, val, rson); push_up(rt); } int query(int ql, int qr, int l, int r, int rt){ if (ql<=l && r<=qr) return maxv[rt]; int m = (l+r)>>1; int maxr = 0; if (ql <= m) maxr = max(maxr, query(ql, qr, lson)); if (qr > m) maxr = max(maxr, query(ql, qr, rson)); return maxr; } int seek(int x, int y){ int maxr = 0; while (top[x] != top[y]){ if (deep[top[x]] < deep[top[y]]) swap(x, y); maxr = max(maxr, query(pos[top[x]], pos[x], 1, n, 1)); x = p[top[x]]; } if (x == y) return maxr; if (deep[x] > deep[y]) swap(x, y); return max(maxr, query(pos[son[x]], pos[y], 1, n, 1)); } int main(){ int T; scanf("%d", &T); while (T--){ scanf("%d", &n); init(); for (int i=1; i<n; ++i){ scanf("%d%d%d", &mess[i].u, &mess[i].v, &mess[i].c); add_edge(mess[i].u, mess[i].v); } dfs1(1, 0); memset(vis, false, sizeof(bool)*(n+1)); dfs2(1, 1); build(1, n, 1); for (int i=1; i<n; ++i){ if (deep[mess[i].u] < deep[mess[i].v]) swap(mess[i].u, mess[i].v); modify(pos[mess[i].u], mess[i].c, 1, n, 1); } int a, b; while (~scanf("%s", op) && op[0]!='D'){ scanf("%d%d", &a, &b); if (op[0] == 'C') modify(pos[mess[a].u], b, 1, n, 1); else printf("%d\n", seek(a, b)); } } return 0; }