SPOJ QTREE 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 abof 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
【思路】

这次问的是点与点之间边权的最大值,思路其实与点权相似,需要做的转化是每一条边都映射一个点,使得除了根节点以外其他所有的点与边是一一对应的,然后边权就能化为点权,以树链剖分后的dfs序用线段树来维护了。


【代码】

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 10005, INF = 0x3f3f3f3f;

struct edge {
	int to, dist, next; 
};

struct segment {
	int left, right, mid, mx;
};

int t, n, cnt, tot;
int head[MAXN], in[MAXN], id[MAXN], point[MAXN], value[MAXN], fa[MAXN], top[MAXN], max_son[MAXN], sz[MAXN], deep[MAXN];
edge e[MAXN << 1];
segment tree[MAXN << 2];

void addedge(int from, int to, int dist)
{
	cnt++;
	e[cnt].to = to;
	e[cnt].dist = dist;
	e[cnt].next = head[from];
	head[from] = cnt;
}

void dfs_1(int u, int father, int depth)
{
	deep[u] = depth;
	fa[u] = father;
	sz[u] = 1;	
	max_son[u] = 0;
	for (int i = head[u]; i != 0; i = e[i].next) {
		int v = e[i].to;
		if (v == fa[u]) continue;
		point[(i + 1) >> 1] = v;
		value[v] = e[i].dist;
		dfs_1(v, u, depth + 1);
		sz[u] += sz[v];
		if (sz[v] > sz[max_son[u]]) max_son[u] = v;
	}
}

void dfs_2(int u, int tp)
{
	in[u] = ++tot;
	id[tot] = u;
	top[u] = tp;
	if (max_son[u] != 0) dfs_2(max_son[u], tp);
	for (int i = head[u]; i != 0; i = e[i].next) {
		int v = e[i].to;
		if (v == fa[u] || v == max_son[u]) continue;
		dfs_2(v, v);
	}
}

void build(int l, int r, int root)
{
	tree[root].left = l;
	tree[root].right = r;
	tree[root].mid = (l + r) >> 1;
	if (l == r) {
		tree[root].mx = value[id[l]];
		return;
	}
	build(l, tree[root].mid, root << 1);
	build(tree[root].mid + 1, r, root << 1 | 1);
	tree[root].mx = max(tree[root << 1].mx, tree[root << 1 | 1].mx);
}

void modify(int index, int num, int root)
{
	if (tree[root].left == tree[root].right) {
		tree[root].mx = num;
		return;
	}
	if (index <= tree[root].mid) modify(index, num, root << 1);
	if (index >= tree[root].mid + 1) modify(index, num, root << 1 | 1);
	tree[root].mx = max(tree[root << 1].mx, tree[root << 1 | 1].mx);
}

int query(int l, int r, int root)
{
	if (l <= tree[root].left && tree[root].right <= r) return tree[root].mx;
	int ans = -INF;
	if (l <= tree[root].mid) ans = max(ans, query(l, r, root << 1));
	if (r >= tree[root].mid + 1) ans = max(ans, query(l, r, root << 1 | 1));
	return ans;
}

int main()
{
	scanf("%d", &t);
	while (t--) {
		cnt = 0;
		memset(head, 0, sizeof(head));
		sz[0] = 0;
		scanf("%d", &n);
		for (int i = 1; i <= n - 1; i++) {
			int a, b, c;
			scanf("%d %d %d", &a, &b, &c);
			addedge(a, b, c);
			addedge(b, a, c);
		}
		dfs_1(1, 0, 1);
		tot = 0;
		dfs_2(1, 1);
		build(1, n, 1);
		char mes[8];
		while (scanf("%s", mes) == 1 && mes[0] != 'D') {
			int a, b;
			scanf("%d %d", &a, &b);
			if (mes[0] == 'C')
				modify(in[point[a]], b, 1);
			else {
				int ans = -INF;
				while (top[a] != top[b]) {
					if (deep[top[a]] < deep[top[b]]) swap(a, b);
					ans = max(ans, query(in[top[a]], in[a], 1));
					a = fa[top[a]];
				}
				if (deep[a] < deep[b]) swap(a, b);
				if (a != b) ans = max(ans, query(in[max_son[b]], in[a], 1));
				printf("%d\n", ans);
			}
		}
	}
	return 0;
}



猜你喜欢

转载自blog.csdn.net/shili_xu/article/details/78896060