题目大意:
给定一棵树后两种操作
修改边权, 询问两点间路径上的边权的最大值
大致思路:
树链剖分水题
剖分之后线段树维护最大值即可, 单点更新区间查询
代码如下:
Result : Accepted Memory : 4403 KB Time : 490 ms
/* * Author: Gatevin * Created Time: 2015/9/8 19:36:25 * File Name: Sakura_Chiyo.cpp */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue> #include<stack> #include<map> #include<set> #include<bitset> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #include<cmath> #include<ctime> #include<iomanip> using namespace std; const double eps(1e-8); typedef long long lint; #define maxn 10010 int top[maxn]; int grandson[maxn]; int dep[maxn]; int siz[maxn]; int belong[maxn]; int father[maxn]; int Q[maxn]; int cnt; int hson[maxn]; int n; bool vis[maxn]; int id[maxn]; int antiID[maxn]; struct Edge { int u, v, w, nex; Edge(int _u, int _v, int _w, int _nex) { u = _u, v = _v, w = _w, nex = _nex; } Edge(){} }; int head[maxn]; int tot; Edge edge[maxn << 1]; int w[maxn]; void add_Edge(int x, int y, int w) { edge[++tot] = Edge(x, y, w, head[x]); head[x] = tot; } void split() { cnt = 0; int l = 0, r = 1; dep[Q[r] = 1] = 1; father[r] = -1; w[r] = 0; while(l < r) { int x = Q[++l]; if(head[x] == -1) continue; for(int j = head[x]; j + 1; j = edge[j].nex) { int y = edge[j].v; if(y == father[x]) continue; w[y] = edge[j].w; dep[Q[++r] = y] = dep[x] + 1; father[y] = x; } } for(int i = n; i; i--) { int x = Q[i], p = -1; siz[x] = 1; if(head[x] == -1) continue; for(int j = head[x]; j + 1; j = edge[j].nex) { int y = edge[j].v; if(y == father[x]) continue; siz[x] += siz[y]; if(p == -1 || (p > 0 && siz[y] > siz[p])) p = y; } if(p == -1) { hson[x] = -1; grandson[++cnt] = x; belong[top[cnt] = x] = cnt; } else { hson[x] = p; belong[x] = belong[p]; top[belong[x]] = x; } } int idx = 0; memset(vis, 0, sizeof(vis)); for(int i = n; i; i--) { int x = Q[i]; if(vis[x]) continue; vis[x] = 1; id[x] = ++idx; antiID[idx] = x; while(father[x] != -1 && belong[father[x]] == belong[x] && !vis[father[x]]) { x = father[x]; id[x] = ++idx; antiID[idx] = x; vis[x] = 1; } } return; } struct Segment_Tree { #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 int ma[maxn << 2]; void pushUp(int rt) { ma[rt] = max(ma[rt << 1], ma[rt << 1 | 1]); return; } void build(int l, int r, int rt) { if(l == r) { ma[rt] = w[antiID[l]]; return; } int mid = (l + r) >> 1; build(lson); build(rson); pushUp(rt); } void update(int l, int r, int rt, int pos, int value) { if(l == r) { ma[rt] = value; return; } int mid = (l + r) >> 1; if(mid >= pos) update(lson, pos, value); else update(rson, pos, value); pushUp(rt); } int query(int l, int r, int rt, int L, int R) { if(l >= L && r <= R) return ma[rt]; int mid = (l + r) >> 1; int ret = -1e9; if(mid >= L) ret = max(ret, query(lson, L, R)); if(mid + 1 <= R) ret = max(ret, query(rson, L, R)); return ret; } }; Segment_Tree st; int answer(int x, int y) { int ans = -1e9; while(top[belong[x]] != top[belong[y]]) { if(dep[top[belong[x]]] < dep[top[belong[y]]]) swap(x, y); ans = max(ans, st.query(1, n, 1, id[x], id[top[belong[x]]])); x = father[top[belong[x]]]; } if(x == y) return ans; if(dep[x] < dep[y]) swap(x, y); ans = max(ans, st.query(1, n, 1, id[x], id[hson[y]])); return ans; } int main() { int T; scanf("%d", &T); while(T--) { scanf("%d", &n); tot = 0; memset(head, -1, sizeof(head)); int u, v, w, x; for(int i = 1; i < n; i++) { scanf("%d %d %d", &u, &v, &w); add_Edge(u, v, w); add_Edge(v, u, w); } split(); st.build(1, n, 1); char op[10]; while(scanf("%s", op)) { if(op[0] == 'D') break; if(op[0] == 'Q') { scanf("%d %d", &u, &v); printf("%d\n", answer(u, v)); } else { scanf("%d %d", &x, &w); u = edge[x << 1].u; v = edge[x << 1].v; if(father[u] == v) st.update(1, n, 1, id[u], w); else st.update(1, n, 1, id[v], w); } } } return 0; }