POJ - 2763 Housewife Wind 题解报告

题解报告

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 n1行,每行描述第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]2pre[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;
}

6.AC截屏

Vjudge的AC截屏

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值