题解报告
1. 题目来源:
POJ - 2763 Housewife Wind
http://poj.org/problem?id=2763
2. 题目原文:
After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beautiful huts. There are some pairs of huts connected by bidirectional roads. We say that huts in the same pair directly connected. XX Village is so special that we can reach any other huts starting from an arbitrary hut. If each road cannot be walked along twice, then the route between every pair is unique.
Since Jiajia earned enough money, Wind became a housewife. Their children loved to go to other kids, then make a simple call to Wind: ‘Mummy, take me home!’
At different times, the time needed to walk along a road may be different. For example, Wind takes 5 minutes on a road normally, but may take 10 minutes if there is a lovely little dog to play with, or take 3 minutes if there is some unknown strange smell surrounding the road.
Wind loves her children, so she would like to tell her children the exact time she will spend on the roads. Can you help her?
Input
The first line contains three integers n, q, s. There are n huts in XX Village, q messages to process, and Wind is currently in hut s. n < 100001 , q < 100001.
The following n-1 lines each contains three integers a, b and w. That means there is a road directly connecting hut a and b, time required is w. 1<=w<= 10000.
The following q lines each is one of the following two types:
Message A: 0 u
A kid in hut u calls Wind. She should go to hut u from her current position.
Message B: 1 i w
The time required for i-th road is changed to w. Note that the time change will not happen when Wind is on her way. The changed can only happen when Wind is staying somewhere, waiting to take the next kid.
Output
For each message A, print an integer X, the time required to take the next child.
Sample Input
3 3 1
1 2 1
2 3 2
0 2
1 2 3
0 3
Sample Output
1
3
3. 题意翻译:
有
n
n
n个点构成的树,每条边都有一个费用,当前位置为
s
s
s点,有
q
q
q种操作。操作
0
0
0:输出当前位置到
u
u
u点的花费(要求不能重复经过一条边,所以路径是唯一的),并将当前位置设置为
u
u
u点。操作
1
1
1:将第
i
i
i调边的费用修改为
w
w
w。
输入:第一行
n
n
n,
q
q
q,
s
s
s。接下来
n
−
1
n-1
n−1行,每行描述第i条边,
a
a
a,
b
b
b,
w
w
w,即有一条边连接
a
a
a,
b
b
b(互相可到达),费用为
w
w
w。接下来
q
q
q行,每行表示一个操作。
0
0
0操作:
0
0
0,
u
u
u。
1
1
1操作:
1
1
1,
i
i
i,
w
w
w;
输出:对于每一个
0
0
0操作,输出花费。
4.解题思路:
由于题目中的权值是边权,我们先用该边连接深度较深的节点来存权值,从而转化为点权。(由于除根节点外每个节点有且仅有一个父节点)
对于 0 0 0操作,首先想到的一般做法是用一个 p r e pre pre数组记录每一个节点到根节点的权值和,查询 u u u到 v v v的总花费时,算出 p r e [ u ] + p r e [ v ] − 2 ∗ p r e [ L C A ( u , v ) ] pre[u]+pre[v]-2*pre[LCA(u,v)] pre[u]+pre[v]−2∗pre[LCA(u,v)]即可。在代码中 L C A LCA LCA是用倍增求的, s t st st数组即为倍增数组。( L C A LCA LCA:最近公共祖先)
对于 1 1 1操作,由于一开始的边权转换,修改一条边即为修改该边连向深度较深节点的权值。而每次修改时,都是对较深节点为根的子树有影响,所以 p r e pre pre数组需要支持动态的修改,而暴力的修改会使复杂度变为 O ( n 2 ) O(n^2) O(n2)。由于是子树,就想到了 d f s dfs dfs序,而且用线段树或树状数组维护的话每次只需 O ( l o g 2 ( n ) ) O(log_2(n)) O(log2(n))。
于是便转化成了区间加值,单点查询的问题了。线段树常数大(太长了),所以推荐树状数组。第一次
d
f
s
dfs
dfs维护深度
d
e
p
t
h
depth
depth,子树范围
l
l
l,
r
r
r和
p
r
e
pre
pre数组。
p
r
e
pre
pre数组不变,每次用一个差分树状数组
t
t
t来维护,比如
i
i
i点加
v
v
v时,
u
p
d
a
t
e
(
l
[
i
]
,
v
)
;
u
p
d
a
t
e
(
r
[
i
]
+
1
,
−
v
)
update(l[i],v);update(r[i]+1,-v)
update(l[i],v);update(r[i]+1,−v);查询
x
x
x,
y
y
y时答案变成
p
r
e
[
x
]
+
g
e
t
s
u
m
(
l
[
x
]
)
+
p
r
e
[
y
]
+
g
e
t
s
u
m
(
l
[
y
]
)
−
2
∗
(
p
r
e
[
L
C
A
(
x
,
y
)
]
+
g
e
t
s
u
m
(
l
[
L
C
A
(
x
,
y
)
]
)
)
pre[x]+getsum(l[x])+pre[y]+getsum(l[y])-2*(pre[LCA(x,y)]+getsum(l[LCA(x,y)]))
pre[x]+getsum(l[x])+pre[y]+getsum(l[y])−2∗(pre[LCA(x,y)]+getsum(l[LCA(x,y)]))(
g
e
t
s
u
m
getsum
getsum指树状数组求前缀和,
u
p
d
a
t
e
update
update指树状数组单点更新)
5.程序代码(含注释)
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <vector>
#include <stack>
#include <cstdlib>
#include <limits.h>
#include <map>
#include <set>
using namespace std;
const int MAXN=1e5+13;
const int Mod=1e9+7;
struct Node{
int v,dis,nxt;
Node(){}
Node(int v,int dis,int nxt):v(v),dis(dis),nxt(nxt){}
}a[MAXN<<1];//用链式前向星存边,head保存边集的最后一条边,cnt表示内存池
int N,q,s,head[MAXN<<1],cnt,u[MAXN],v[MAXN],w[MAXN],depth[MAXN],pre[MAXN];//u、v、w存第i条边的信息,depth深度,pre求到根的总花费
int l[MAXN],r[MAXN],time,st[MAXN][21],f[MAXN],t[MAXN];//f存父节点,t树状数组,st倍增数组,存祖先,l、r存dfs序,time是dfs序的时间戳
void dfs(int rt,int fa,int sum) {
l[rt]=++time;
f[rt]=fa;
for(int i=head[rt];~i;i=a[i].nxt) {
int v=a[i].v,dis=a[i].dis;
if(v==fa) continue;
pre[v]=sum+dis;
depth[v]=depth[rt]+1;
dfs(v,rt,pre[v]);
}
r[rt]=time;
return;
}//求dfs序,深度,树上前缀
int lca(int x,int y) {
if(depth[x]<depth[y]) swap(x,y);
for(int i=20;i>=0;i--) {
if(depth[x]-(1<<i)>=depth[y]) {
x=st[x][i];
}
}
if(x==y) return x;
for(int i=20;i>=0;i--) {
if(depth[x]-(1<<i)>=0&&st[x][i]!=st[y][i]) {
x=st[x][i];
y=st[y][i];
}
}
return f[x];
}//倍增求最近公共祖先
int lowbit(int x) {
return x&-x;
}
int get_sum(int x) {
int sum=0;
for(;x>0;x-=lowbit(x)) {
sum+=t[x];
}
return sum;
}//树状数组求前缀和
void update(int x,int v) {
for(;x<=N;x+=lowbit(x)) {
t[x]+=v;
}
return;
}//树状数组单点更新
int main()
{
while(scanf("%d%d%d",&N,&q,&s)!=EOF) {
cnt=0;time=0;
memset(t,0,sizeof(t));
memset(depth,0,sizeof(depth));
memset(pre,0,sizeof(pre));
memset(l,0,sizeof(l));
memset(r,0,sizeof(r));
memset(f,-1,sizeof(f));
memset(a,-1,sizeof(a));
memset(head,-1,sizeof(head));
memset(st,-1,sizeof(st));//初始化
for(int i=1;i<N;i++) {
scanf("%d%d%d",&u[i],&v[i],&w[i]);
a[++cnt]=Node(v[i],w[i],head[u[i]]);
head[u[i]]=cnt;
a[++cnt]=Node(u[i],w[i],head[v[i]]);
head[v[i]]=cnt;
}//存边
dfs(1,-1,0);
for(int i=1;i<=N;i++) {
st[i][0]=f[i];
}//初始化st表
for(int i=1;i<=20;i++) {
for(int j=1;j<=N;j++) {
if(depth[j]-(1<<i)>=0) {
st[j][i]=st[st[j][i-1]][i-1];
}
}
}//算st表
for(int i=1;i<=q;i++) {
int p,x,y;
scanf("%d",&p);
if(p==1) {
scanf("%d%d",&x,&y);
int add=y-w[x];
w[x]=y;
int V=depth[u[x]]>depth[v[x]]?u[x]:v[x];//V指深度较深的节点,用于把边权转为点权
update(l[V],add);
update(r[V]+1,-add);
}
else {
scanf("%d",&x);
y=s;
int anc=lca(x,y);
// cout<<get_sum(l[x])<<' '<<get_sum(l[y])<<' '<<get_sum(l[anc])<<endl;
printf("%d\n",pre[x]+get_sum(l[x])+pre[y]+get_sum(l[y])-2*(pre[anc]+get_sum(l[anc])));
s=x;
}
}
}
return 0;
}