不解释,直接给代码。
#include <stdio.h>
#include <string.h>
#include <algorithm>
int rd()
{
int k = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
f = 0;
c = getchar();
}
while (c >= '0' && c <= '9')
{
k = (k << 1) + (k << 3) + (c ^ 48);
c = getchar();
}
return f ? k : -k;
}
void wr(int x)
{
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
wr(x / 10);
putchar((x % 10) ^ '0');
}
char rd_char()
{
char c = getchar();
while (c < 'A' || c > 'Z')
c = getchar();
return c;
}
const int N = 10010;
int a[N];
namespace seg
{
int *rev_dfn;
int n;
int lc(int u) { return u << 1; }
int rc(int u) { return (u << 1) | 1; }
int mx[N << 2];
void pushup(int u) { mx[u] = std::max(mx[lc(u)], mx[rc(u)]); }
void _build(int u, int l, int r)
{
if (l == r)
mx[u] = a[rev_dfn[l]];
else
{
int m = (l + r) >> 1;
_build(lc(u), l, m), _build(rc(u), m + 1, r);
pushup(u);
}
}
void build(int _n) { n = _n, _build(1, 1, n); }
void _modify(int u, int L, int R, int pos, int val)
{
if (L == R)
mx[u] = val;
else
{
int M = (L + R) >> 1;
if (pos <= M)
_modify(lc(u), L, M, pos, val);
else
_modify(rc(u), M + 1, R, pos, val);
pushup(u);
}
}
void modify(int pos, int val) { _modify(1, 1, n, pos, val); }
int _query(int u, int l, int r, int L, int R)
{
if (L > r || R < l || l > r)
return 0;
if (l <= L && R <= r)
return mx[u];
int M = (L + R) >> 1;
return std::max(_query(lc(u), l, r, L, M), _query(rc(u), l, r, M + 1, R));
}
int query(int l, int r) { return _query(1, l, r, 1, n); }
}
namespace HLD
{
int n, rt;
struct edge
{
int to, w, nxt;
} e[N << 1];
int h[N], ecnt;
std::pair<int, int> eg[N];
void add_edge(int u, int v, int w) { e[++ecnt].to = v, e[ecnt].w = w, e[ecnt].nxt = h[u], h[u] = ecnt; }
void add_undirect(int i, int u, int v, int w) { add_edge(u, v, w), add_edge(v, u, w), eg[i] = std::make_pair(u, v); }
int f[N], sz[N], dep[N], son[N];
int dfn[N], rev_dfn[N], top[N], dfs_t;
void clr()
{
ecnt = dfs_t = 0;
memset(h, 0, sizeof(h)), memset(son, 0, sizeof(son));
memset(top, 0, sizeof(top));
}
void dfs1(int u, int fa)
{
f[u] = fa, sz[u] = 1, dep[u] = dep[fa] + 1;
for (int i = h[u]; i; i = e[i].nxt)
{
int v = e[i].to, w = e[i].w;
if (v == fa)
a[u] = w;
else
{
dfs1(v, u), sz[u] += sz[v];
if (sz[son[u]] < sz[v])
son[u] = v;
}
}
}
void dfs2(int u, int topf) // initial rt, rt
{
top[u] = topf, dfn[u] = ++dfs_t, rev_dfn[dfs_t] = u;
if (son[u])
dfs2(son[u], topf);
for (int i = h[u]; i; i = e[i].nxt)
{
int v = e[i].to;
if (v == f[u] || v == son[u])
continue;
dfs2(v, v);
}
}
void build(int _rt, int _n)
{
n = _n, rt = _rt;
dfs1(rt, 0), dfs2(rt, rt), seg::rev_dfn = rev_dfn;
seg::build(n);
}
void modify_edge(int id, int val)
{
int u = dep[eg[id].first] > dep[eg[id].second] ? eg[id].first : eg[id].second;
seg::modify(dfn[u], val);
}
int query(int u, int v)
{
int ret = 0;
while (top[u] ^ top[v])
{
if (dep[f[top[u]]] < dep[f[top[v]]])
std::swap(u, v);
ret = std::max(ret, seg::query(dfn[top[u]], dfn[u]));
u = f[top[u]];
}
if (dep[u] > dep[v])
std::swap(u, v);
ret = std::max(ret, seg::query(dfn[u] + 1, dfn[v]));
return ret;
}
}
int main()
{
int T = rd();
while (T--)
{
HLD::clr();
int n = rd();
for (int i = 1; i < n; ++i)
{
int u = rd(), v = rd(), w = rd();
HLD::add_undirect(i, u, v, w);
}
HLD::build(1, n);
char op = 'X';
int u, v;
while ((op = rd_char()) ^ 'D')
{
u = rd(), v = rd();
if (op == 'C')
HLD::modify_edge(u, v);
else
wr(HLD::query(u, v)), putchar('\n');
}
}
}