题意:给定一棵树,然后有一些操作,有两种,一种是改变某条边的权值,第二种是询问从u->v的路径中,边权最大的是多少。
析:就一个树链剖分,然后用线段树维护即可。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const LL LNF = 1e16; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 20000 + 10; const int mod = 1e9 + 7; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } struct Edge{ int to, next; }; Edge edges[maxn<<1]; int p[maxn], fp[maxn], son[maxn], top[maxn]; int dp[maxn], fa[maxn], head[maxn], num[maxn]; int pos, cnt; int maxv[maxn<<2]; void init(){ pos = 1; cnt = 0; memset(maxv, 0, sizeof maxv); memset(son, -1, sizeof son); memset(head, -1, sizeof head); } void add_edge(int u, int v){ edges[cnt].to = v; edges[cnt].next = head[u]; head[u] = cnt++; } void dfs1(int u, int pre, int d){ dp[u] = d; fa[u] = pre; num[u] = 1; for(int i = head[u]; ~i; i = edges[i].next){ int v = edges[i].to; if(v == pre) continue; dfs1(v, u, d+1); num[u] += num[v]; if(son[u] == -1 || num[v] > num[son[u]]) son[u] = v; } } void dfs2(int u, int sp){ top[u] = sp; p[u] = pos++; fp[p[u]] = u; if(son[u] == -1) return ; dfs2(son[u], sp); for(int i = head[u]; ~i; i = edges[i].next){ int v = edges[i].to; if(v != fa[u] && v != son[u]) dfs2(v, v); } } int e[maxn][3]; void update(int x, int val, int l, int r, int rt){ if(x == l && r == x){ maxv[rt] = val; return ; } int m = l+r >> 1; if(x <= m) update(x, val, lson); else update(x, val, rson); maxv[rt] = max(maxv[rt<<1], maxv[rt<<1|1]); } int query(int L, int R, int l, int r, int rt){ if(L <= l && r <= R) return maxv[rt]; int ans = 0; int m = l+r >> 1; if(L <= m) ans = max(ans, query(L, R, lson)); if(R > m) ans = max(ans, query(L, R, rson)); return ans; } int solve(int u, int v){ int ans = 0; int f1 = top[u], f2 = top[v]; while(f1 != f2){ if(dp[f1] < dp[f2]){ swap(f1, f2); swap(u, v); } ans = max(ans, query(p[f1], p[u], 1, n, 1)); u = fa[f1]; f1 = top[u]; } if(u == v) return ans; if(dp[u] > dp[v]) swap(u, v); return max(ans, query(p[son[u]], p[v], 1, n, 1)); } int main(){ int T; cin >> T; while(T--){ scanf("%d", &n); init(); for(int i = 1; i < n; ++i){ scanf("%d %d %d", &e[i][0], &e[i][1], &e[i][2]); add_edge(e[i][0], e[i][1]); add_edge(e[i][1], e[i][0]); } dfs1(1, 1, 0); dfs2(1, 1); for(int i = 1; i < n; ++i){ if(dp[e[i][0]] > dp[e[i][1]]) swap(e[i][0], e[i][1]); update(p[e[i][1]], e[i][2], 1, n, 1); } char s[10]; while(scanf("%s", s) == 1 && s[0] != 'D'){ int u, v; scanf("%d %d", &u, &v); if(s[0] == 'Q') printf("%d\n", solve(u, v)); else update(p[e[u][1]], v, 1, n, 1); } } return 0; }