您的位置:首页 > 产品设计 > UI/UE

SPOJ QTREE - Query on a tree 【树链剖分】

2016-01-27 15:53 459 查看


QTREE - Query on a tree

#number-theory

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

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

Output:
1
3


讲的很好:点我  自己模拟下查询过程就清楚了。

son[]记录重儿子,dep[]记录深度,num[]记录孩子个数,pre[]记录父节点。

top[]记录重链头节点,pos[]记录节点与父亲连边在线段树中的位置。

更新时直接更新,保证u -> v的连边u是父亲。

查询时<u, v>时,限制top[u] > top[v](反过来也可以),沿着u和它父亲的链向上找,边找边更新,到达v所在的链头节点top[v],若不等于v,再更新top[v] - v链上的最大值。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (10000+10)
#define MAXM (300000+10)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while((a)--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
#pragma comment(linker, "/STACK:102400000,102400000")
#define fi first
#define se second
using namespace std;
struct Tree{
int l, r, Max;
};
Tree tree[MAXN<<2];
void PushUp(int o){
tree[o].Max = max(tree[ll].Max, tree[rr].Max);
}
void Build(int o, int l, int r)
{
tree[o].l = l; tree[o].r = r;
tree[o].Max = 0;
if(l == r)
return ;
int mid = (l + r) >> 1;
Build(lson); Build(rson);
}
void Update(int o, int pos, int v)
{
if(tree[o].l == tree[o].r)
{
tree[o].Max = v;
return ;
}
int mid = (tree[o].l + tree[o].r) >> 1;
if(pos <= mid) Update(ll, pos, v);
else Update(rr, pos, v);
PushUp(o);
}
int Query(int o, int L, int R)
{
if(tree[o].l == L && tree[o].r == R)
return tree[o].Max;
int mid = (tree[o].l + tree[o].r) >> 1;
if(R <= mid) return Query(ll, L, R);
else if(L > mid) return Query(rr, L, R);
else return max(Query(ll, L, mid), Query(rr, mid+1, R));
}
struct Edge{
int from, to, val, next;
};
Edge edge[MAXN<<1];
int head[MAXN], edgenum;
int s[MAXN], e[MAXN], c[MAXN];
void init(){
edgenum = 0; CLR(head, -1);
}
void addEdge(int u, int v, int w)
{
Edge E = {u, v, w, head[u]};
edge[edgenum] = E;
head[u] = edgenum++;
}
int son[MAXN], num[MAXN];
int top[MAXN], pos[MAXN], id;
int dep[MAXN], pre[MAXN];
void DFS1(int u, int fa, int d)
{
dep[u] = d; pre[u] = fa; num[u] = 1; son[u] = -1;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(v == fa) continue;
DFS1(v, u, d+1);
num[u] += num[v];
if(son[u] == -1 || num[son[u]] < num[v])
son[u] = v;
}
}
void DFS2(int u, int T)
{
top[u] = T; pos[u] = ++id;
if(son[u] == -1) return ;
DFS2(son[u], T);
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(v == pre[u] || v == son[u]) continue;
DFS2(v, v);
}
}
int GetMax(int u, int v)
{
int f1 = top[u], f2 = top[v];
int ans = 0;
while(f1 != f2)
{
if(dep[f1] < dep[f2])
{
swap(u, v);
swap(f1, f2);
}
ans = max(ans, Query(1, pos[f1], pos[u]));
u = pre[f1], f1 = top[u];
}
if(u == v) return ans;
if(dep[u] > dep[v]) swap(u, v);
return max(ans, Query(1, pos[son[u]], pos[v]));
}
int main()
{
int t; Ri(t);
W(t)
{
int n; Ri(n); init();
for(int i = 1; i <= n-1; i++)
{
Ri(s[i]), Ri(e[i]), Ri(c[i]);
addEdge(s[i], e[i], c[i]);
addEdge(e[i], s[i], c[i]);
}
DFS1(1, -1, 1); id = 0; DFS2(1, 1); Build(1, 1, id);
for(int i = 1; i <= n-1; i++)
{
if(dep[s[i]] > dep[e[i]])
swap(s[i], e[i]);
Update(1, pos[e[i]], c[i]);
}
char str[10];
while(Rs(str), strcmp(str, "DONE"))
{
int x, y; Ri(x); Ri(y);
if(str[0] == 'Q')
Pi(GetMax(x, y));
else
Update(1, pos[e[x]], y);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: