QTREE - Query on a tree
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
树链剖分第二题,此题给出边的权值,可以用边的子顶点树链剖分ID表示边,然后将这些边放入线段树最低层维护。
建树是从num[2]--num[n]上建立线段树。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <bitset>
#include <string>
#include <vector>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define maxn 10010
#define ll long long
#define inf 0x7fffffff
using namespace std;
int n, m, q;
int num[maxn];
//邻接表
struct edge
{
int u, v, w;
int next;
} e[maxn * 2];
int cnt, tim;
int pre[maxn];
void add_edge(int u, int v, int w)
{
e[cnt].u = u;
e[cnt].v = v;
e[cnt].w = w;
e[cnt].next = pre[u];
pre[u] = cnt++;
e[cnt].u = v;
e[cnt].v = u;
e[cnt].w = w;
e[cnt].next = pre[v];
pre[v] = cnt++;
}
//树链剖分
int dep[maxn], fa[maxn], siz[maxn], son[maxn];
// 深度数组 父节点 子节点数目 重儿子
int tid[maxn], ran[maxn], top[maxn];
// 节点的编号 编号的节点 所在链顶端节点
void dfs1(int u, int father, int d)
{
dep[u] = d;
fa[u] = father;
siz[u] = 1;
for (int i = pre[u]; ~i; i = e[i].next)
{
int v = e[i].v;
if (v != father)
{
dfs1(v, u, d + 1);
siz[u] += siz[v];
if (son[u] == -1 || siz[v] > siz[son[u]])
{
son[u] = v;
}
}
}
}
void dfs2(int u, int tp)
{
top[u] = tp;
tid[u] = ++tim;
ran[tid[u]] = u;
if (son[u] == -1)
{
return;
}
dfs2(son[u], tp);
for (int i = pre[u]; ~i; i = e[i].next)
{
int v = e[i].v;
if (v != son[u] && v != fa[u])
{
dfs2(v, v);
}
}
}
//线段树
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int NV = 10005;
int sum[NV << 2];
void PushUp(int rt)
{
sum[rt] = max(sum[rt << 1] , sum[rt << 1 | 1]);
}
void build(int l, int r, int rt = 1)
{
if (l == r)
{
sum[rt] = num[l];
return ;
}
int m = (l + r) >> 1;
build(lson);
build(rson);
PushUp(rt);
}
void update(int L, int c, int l, int r, int rt = 1)
{
if (L == l && l == r)
{
sum[rt] = c;
return ;
}
int m = (l + r) >> 1;
if (L <= m)
{
update(L , c , lson);
}
else
{
update(L , c , rson);
}
PushUp(rt);
}
int query(int L, int R, int l, int r, int rt = 1)
{
if (L <= l && r <= R)
{
return sum[rt];
}
int m = (l + r) >> 1;
int ret = -inf;
if (L <= m)
{
ret = max(ret, query(L , R , lson));
}
if (m < R)
{
ret = max(ret, query(L , R , rson));
}
return ret;
}
int change(int x, int y)
{
int ans = -inf;
while (top[x] != top[y])
{
if (dep[top[x]] < dep[top[y]])
{
swap(x, y);
}
ans = max(ans, query(tid[top[x]], tid[x], 2, n, 1));
x = fa[top[x]];
}
if (dep[x] > dep[y])
{
swap(x, y);
}
if (x != y)
{
ans = max(ans, query(tid[x] + 1, tid[y], 2, n, 1)); //注意加1,因为tid[x]是x上方边的标号
}
return ans;
}
void init()
{
cnt = 0;
tim = 0;
memset(pre, -1, sizeof(pre));
memset(son, -1, sizeof(son));
}
int main()
{
int t;
scanf("%d", &t);
char str[15];
while (t--)
{
init();
int a, b, c;
scanf("%d", &n);
for (int i = 1; i < n; i++)
{
scanf("%d %d %d", &a, &b, &c);
add_edge(a, b, c);
}
dfs1(1, 0, 1);
dfs2(1, 1);
//用边的子顶点编号表示边
for (int i = 0; i < cnt; i++)
{
if (dep[e[i].v] > dep[e[i].u])
{
num[tid[e[i].v]] = e[i].w;
}
else
{
num[tid[e[i].u]] = e[i].w;
}
}
build(2, n, 1);
while (1)
{
scanf(" %s", str);
if (str[0] != 'D')
{
int a, b;
scanf("%d %d", &a, &b);
if (str[0] == 'Q')
{
printf("%d\n", change(a, b));
}
else
{
int pos;
if (dep[e[2 * a - 1].u] < dep[e[2 * a - 1].v])
{
pos = tid[e[2 * a - 1].v];
}
else
{
pos = tid[e[2 * a - 1].u];
}
update(pos, b, 2, n, 1);
}
}
else
{
break;
}
}
}
return 0;
}