kyeremal-spoj375-Query on a tree-树链剖分

5 篇文章 0 订阅
2 篇文章 0 订阅

spoj275-Query on a tree

原题:

QTREE - Query on a tree

no tags 

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 ab 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:
1

3
1 2 1
2 3 2
QUERY 1 2
CHANGE 1 3
QUERY 1 2
DONE

Output:
1
3
大意:给定一棵树,求两点间路径边权的最大值,或修改某条边的边权.

裸的树链剖分


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 1010

int 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值