kyeremal-spoj375-Query on a tree-树链剖分
来源:互联网 发布:linux中more命令 编辑:程序博客网 时间:2024/05/23 13:31
spoj275-Query on a tree
原题:
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:131 2 12 3 2QUERY 1 2CHANGE 1 3QUERY 1 2DONEOutput:13
裸的树链剖分
code:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <algorithm>using namespace std;#define rep(i, l, r) for (int i = l; i <= r; i++)#define REP(i, l, r) for (int i = l; i >= r; i--)#define INF 19971228#define MAXN 1010int n, m = 0, root, N = -1, siz[MAXN], dep[MAXN], son[MAXN], fat[MAXN], top[MAXN], w[MAXN], first[MAXN], next[MAXN], num[MAXN], sb[MAXN];int bh[MAXN], M = 0, edg[MAXN];struct tlist{int x, y, t;} a[MAXN];bool vis[MAXN];struct Tree{int l, r, mx, lc, rc;} tree[MAXN];inline void add(int x, int y, int t) {a[++N].x = x, a[N].y = y, a[N].t = t, next[N] = first[x], first[x] = N;}inline int min(int a, int b) {return a<b ? a : b;}inline int max(int a, int b) {return a>b ? a : b;}inline void dfs(int x, int DEP) { siz[x] = 1; dep[x] = DEP; vis[x] = 1; int maxsize = 0; for (int i = first[x]; ~i; i = next[i])if (!vis[a[i].y]) { fat[a[i].y] = x; edg[a[i].y] = i; dfs(a[i].y, DEP+1); siz[x] += siz[a[i].y]; if (siz[a[i].y] > maxsize) maxsize = siz[a[i].y], son[x] = a[i].y, sb[x] = i;}}inline void DFS(int x, int T) { vis[x] = 1; top[x] = T; if (son[x]) w[sb[x]] = ++m, num[m] = sb[x], DFS(son[x], T); for (int i = first[x]; ~i; i = next[i])if (!vis[a[i].y]) w[i] = ++m, num[m] = i, DFS(a[i].y, a[i].y);}inline void build_tree(int i, int L, int R) { tree[i].l = L, tree[i].r = R; if (L == R) {tree[i].mx = a[num[L]].t; return;} build_tree(tree[i].lc = ++M, L, (L+R) >> 1); build_tree(tree[i].rc = ++M, ((L+R) >> 1) + 1, R); tree[i].mx = max(tree[tree[i].lc].mx, tree[tree[i].rc].mx);}inline void modify(int i, int x, int cx) { int L = tree[i].l, R = tree[i].r; if (x < L || x > R) return; if (L == R) {tree[i].mx = cx; return;} modify(tree[i].lc, x, cx); modify(tree[i].rc, x, cx); tree[i].mx = max(tree[tree[i].lc].mx, tree[tree[i].rc].mx);}inline int query(int i, int ql, int qr) { int L = tree[i].l, R = tree[i].r; if (qr < L || ql > R) return -INF; if (ql <= L && qr >= R) return tree[i].mx; return max(query(tree[i].lc, ql, qr), query(tree[i].rc, ql, qr));}inline int get_edge(int i) {return w[bh[i]] ? w[bh[i]] : w[bh[i]+1];}int main() { cin >> n; memset(first, -1, sizeof(first)); memset(next, -1, sizeof(next)); rep(i, 1, n-1) {int tx, ty, tt;scanf("%d%d%d", &tx, &ty, &tt);fat[ty] = tx;if (!fat[tx]) root = tx;bh[i] = N + 1;add(tx, ty, tt);add(ty, tx, tt); } memset(vis, 0, sizeof(vis)); dfs(root, 1); memset(vis, 0, sizeof(vis)); memset(w, 0, sizeof(w)); memset(num, 0, sizeof(num)); DFS(root, root); build_tree(M = 1, 1, m); while (1) {char ch[MAXN];int tx, ty;scanf("%s%d%d", ch, &tx, &ty);if (ch[0] == 'D') break;if (ch[0] == 'C') modify(1, get_edge(tx), ty);if (ch[0] == 'Q') { int f1, f2, ans = -INF; while (tx != ty) {f1 = top[tx], f2 = top[ty];if (f1 != f2) { if (dep[f1] < dep[f2]) swap(f1, f2), swap(tx, ty); ans = max(ans, query(1, w[edg[f1]], w[edg[tx]])), tx = fat[f1];}else { if (dep[tx] < dep[ty]) swap(tx, ty); ans = max(ans, query(1, w[edg[son[ty]]], w[edg[tx]])), tx = ty;} } cout << ans << endl;} } return 0;}
1 0
- kyeremal-spoj375-Query on a tree-树链剖分
- SPOJ375--Query on a tree(树链剖分)
- spoj375 Query on a tree 树链剖分
- spoj375 Query on a tree 树链剖分
- 【树链剖分】spoj375 Query on a tree
- spoj375 Query on a tree(树链剖分)
- 【SPOJ375】Query on a tree-树链剖分
- [SPOJ375]Query on a tree
- SPOJ375 Query on a tree
- SPOJ375 Query on a tree
- spoj375 Query on a tree
- 树链剖分+线段树 spoj375 Query on a tree
- 【SPOJ375】 Query on a tree——树链剖分
- Spoj375 Query on a tree 裸·树链剖分
- SPOJ375 QTREE - Query on a tree
- Spoj375.Query on a tree——树链剖分+线段树(基于边权)
- 【spoj375】Query on a tree【树链剖分】【或者动态树,那样常数就完了T_T】
- SPOJ375——Query on a tree(树链剖分模板详解以及入门)
- construct2中对象的物理属性(Physics Behaviors)
- Ubuntu下安装NodeJS的多种办法
- Android 屏幕适配方案
- 关于导入struts2的jar后会报各种错
- 黑马程序员——Java练习笔记——多线程
- kyeremal-spoj375-Query on a tree-树链剖分
- 移动App服务端架构设计
- 1、Linux操作系统之操作篇-逻辑卷LVM使用命令
- nmake命令(windows下的makefile)
- Android 编程下 Touch 事件的分发和消费机制
- 设计模式(22)——Template Method(模板方法)
- Spring 错误!
- 黑马程序员---银行业务调度系统
- SQL Server事務編程相關總結