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.
spo就绝对是最让人自闭的oj!!!!!!!!
ac代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 10005;
struct Edge {
int u, v, w;
} e[maxn];
vector<int> G[maxn];
int tot;
int sz[maxn], dep[maxn];
int ch[maxn], fa[maxn];
int top[maxn], tid[maxn];
int sum[maxn << 2];
void dfs1(int u, int f, int d) {
sz[u] = 1;
fa[u] = f;
dep[u] = d;
for(int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if(v == f) {
continue;
}
dfs1(v, u, d + 1);
sz[u] += sz[v];
if(ch[u] == -1 || sz[v] > sz[ch[u]]) {
ch[u] = v;
}
}
}
void dfs2(int u, int tp) {
top[u] = tp;
tid[u] = ++tot;
if(ch[u] == -1) {
return;
}
dfs2(ch[u], tp);
for(int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if(v != ch[u] && v != fa[u]) {
dfs2(v, v);
}
}
}
void pushUp(int i) {
sum[i] = max(sum[i << 1], sum[i << 1 | 1]);
}
void init(int i, int l, int r) {
sum[i] = 0;
if(l == r) {
return;
}
int mid = (l + r) >> 1;
init(i << 1, l, mid);
init(i << 1 | 1, mid + 1, r);
}
void update(int i, int l, int r, int pos, int val) {
if(l == r) {
sum[i] = val;
return;
}
int mid = (l + r) >> 1;
if(pos <= mid) {
update(i << 1, l, mid, pos, val);
} else {
update(i << 1 | 1, mid + 1, r, pos, val);
}
pushUp(i);
}
int query(int i, int l, int r, int L, int R) {
if(l >= L && r <= R) {
return sum[i];
}
int mid = (l + r) >> 1;
int a = -1, b = -1;
if(L <= mid) {
a = query(i << 1, l, mid, L, R);
}
if(R > mid) {
b = query(i << 1 | 1, mid + 1, r, L, R);
}
return max(a, b);
}
int solve(int u, int v, int n) {
int f1 = top[u], f2 = top[v];
// 不在同一条链上
int ans = 0;
while(f1 != f2) {
if(dep[f1] < dep[f2]) {
swap(f1, f2);
swap(u, v);
}
// update(1, 1, n, tid[f1], tid[u], val);
ans = max(ans, query(1, 1, n, tid[f1], tid[u]));
// 一点点往上爬
u = fa[f1];
f1 = top[u];
}
if(u == v) {
return ans;
}
if(dep[u] > dep[v]) {
swap(u, v);
}
// 同一链时,直接更新区间
// update(1, 1, n, tid[v], tid[u], val);
return max(ans, query(1, 1, n, tid[ch[u]], tid[v]));
}
int main() {
int t, n;
char op[10];
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
tot = 0;
memset(ch, -1, sizeof(ch));
for(int i = 1; i <= n; i++) {
G[i].clear();
}
for(int i = 1; i <= n - 1; i++) {
scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
G[e[i].u].push_back(e[i].v);
G[e[i].v].push_back(e[i].u);
}
dfs1(1, 0, 0);
dfs2(1, 1);
init(1, 1, n);
for(int i = 1; i <= n - 1; i++) {
if(dep[e[i].u] > dep[e[i].v]) {
swap(e[i].u, e[i].v);
}
update(1, 1, n, tid[e[i].v], e[i].w);
}
int u, v;
while(~scanf("%s", op)) {
if(op[0] == 'D') {
break;
}
scanf("%d%d", &u, &v);
if(op[0] == 'C') {
update(1, 1, n, tid[e[u].v], v);
} else {
printf("%d\n", solve(u, v, n));
}
}
}
return 0;
}

本文介绍了一种处理树状结构数据的高效算法,通过离线处理的方式,实现了对树中边的权重进行修改(CHANGE操作)和查询任意两点间路径上的最大边权重(QUERY操作)。采用线段树和重链剖分技术,有效地解决了大规模树形结构数据的动态维护问题。

被折叠的 条评论
为什么被折叠?



