边权操作起来也和点权一样,只要把边的权值映射到点上即可,要注意的地方是向上爬的过程中和点权不太一样,还有个特判(WA了几次。。)

完整代码

  1. #include<cstring>
  2. #include<cstdio>
  3. #include<iostream>
  4. using namespace std;
  5. #define maxn 100005
  6. struct E{int x,y;}e[maxn];
  7. struct Edge{int to,nxt,w;}edge[maxn<<];
  8. int head[maxn],tot,v[maxn],n,q,c;
  9. void init(){memset(head,-,sizeof head);tot=;}
  10. void addedge(int u,int v,int w){
  11. edge[tot].to=v;edge[tot].w=w;edge[tot].nxt=head[u];head[u]=tot++;
  12. }
  13. int f[maxn],son[maxn],d[maxn],size[maxn];
  14. void dfs1(int x,int pre,int deep){
  15. size[x]=,d[x]=deep,f[x]=pre;
  16. for(int i=head[x];i!=-;i=edge[i].nxt){
  17. int y=edge[i].to;
  18. if(y==pre)continue;
  19. v[y]=edge[i].w;//注意这里将边权映射到点上
  20. dfs1(y,x,deep+);
  21. size[x]+=size[y];
  22. if(size[y]>size[son[x]])son[x]=y;
  23. }
  24. }
  25. int cnt,top[maxn],id[maxn],rk[maxn];
  26. void dfs2(int x,int tp){
  27. top[x]=tp;id[x]=++cnt;rk[cnt]=x;
  28. if(son[x])dfs2(son[x],tp);
  29. for(int i=head[x];i!=-;i=edge[i].nxt){
  30. int y=edge[i].to;
  31. if(y!=f[x] && y!=son[x])dfs2(y,y);
  32. }
  33. }
  34.  
  35. #define lson l,m,rt<<1
  36. #define rson m+1,r,rt<<1|1
  37. int sum[maxn<<];
  38. void pushup(int rt){sum[rt]=sum[rt<<]+sum[rt<<|];}
  39. void build(int l,int r,int rt){
  40. if(l==r){sum[rt]=v[rk[l]];return;}
  41. int m=l+r>>;
  42. build(lson);build(rson);
  43. pushup(rt);
  44. }
  45. void update(int pos,int l,int r,int rt,int v){
  46. if(l==r){sum[rt]=v;return;}
  47. int m=l+r>>;
  48. if(pos<=m)update(pos,lson,v);
  49. else update(pos,rson,v);
  50. pushup(rt);
  51. }
  52. int query(int L,int R,int l,int r,int rt){
  53. if(L<=l && R>=r)return sum[rt];
  54. int m=l+r>>,res=;
  55. if(L<=m)res+=query(L,R,lson);
  56. if(R>m)res+=query(L,R,rson);
  57. return res;
  58. }
  59.  
  60. int Query(int x,int y){
  61. int res=;
  62. while(top[x]!=top[y]){
  63. if(d[top[x]]<d[top[y]])swap(x,y);
  64. res+=query(id[top[x]],id[x],,n,);
  65. x=f[top[x]];
  66. }
  67. if(x==y)return res;
  68. if(id[x]>id[y])swap(x,y);
  69. return res+query(id[son[x]],id[y],,n,);
  70. }
  71.  
  72. int main(){
  73. init();int s;
  74. scanf("%d%d%d",&n,&q,&s);
  75. for(int i=;i<n;i++){
  76. int x,y,z;scanf("%d%d%d",&x,&y,&z);
  77. e[i].x=x;e[i].y=y;
  78. addedge(x,y,z);addedge(y,x,z);
  79. }
  80. cnt=;dfs1(s,,);dfs2(s,s);
  81. build(,n,);
  82. while(q--){
  83. int op,x,y;
  84. scanf("%d",&op);
  85. if(op==){scanf("%d",&x);
  86. cout<<Query(s,x)<<'\n';s=x;}
  87. if(op==){
  88. scanf("%d%d",&x,&y);
  89. int u=e[x].x,v=e[x].y;
  90. if(d[u]<d[v])swap(u,v);
  91. update(id[u],,n,,y);
  92. }
  93. }
  94. }

不同之处:最后一次查询的时候不要最高的那个点权

特判:x==y的时候直接返回即可

  1. int Query(int x,int y){
  2. int res=;
  3. while(top[x]!=top[y]){
  4. if(d[top[x]]<d[top[y]])swap(x,y);
  5. res+=query(id[top[x]],id[x],,n,);
  6. x=f[top[x]];
  7. }
  8. if(x==y)return res;
  9. if(id[x]>id[y])swap(x,y);
  10. return res+query(id[son[x]],id[y],,n,);
  11. }

树链剖分——边权poj2763的更多相关文章

  1. POJ2763 Housewife Wind 树链剖分 边权

    POJ2763 Housewife Wind 树链剖分 边权 传送门:http://poj.org/problem?id=2763 题意: n个点的,n-1条边,有边权 修改单边边权 询问 输出 当前 ...

  2. BZOJ 1036 [ZJOI2008]树的统计Count (树链剖分 - 点权剖分 - 单点权修改)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1036 树链剖分模版题,打的时候注意点就行.做这题的时候,真的傻了,单词拼错检查了一个多小时 ...

  3. 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)

    Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...

  4. POJ3237 Tree 树链剖分 边权

    POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...

  5. HDU3669 Aragorn's Story 树链剖分 点权

    HDU3669 Aragorn's Story 树链剖分 点权 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: n个点的,m条边,每个点都 ...

  6. BZOJ 1984: 月下“毛景树” [树链剖分 边权]

    1984: 月下“毛景树” Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 1728  Solved: 531[Submit][Status][Discu ...

  7. SPOJ 375 (树链剖分 - 边权剖分 - 修改单边权)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28982#problem/I 给你一棵有边权的树,有两个操作:一个操作是输出l到 ...

  8. 树链剖分边权模板spoj375

    树链剖分是树分解成多条链来解决树上两点之间的路径上的问题 如何求出树链:第一次dfs求出树上每个结点的大小和深度和最大的儿子,第二次dfs就能将最大的儿子串起来并hash(映射)到线段树上(或者其他数 ...

  9. 洛谷 P3384 【模板】树链剖分-树链剖分(点权)(路径节点更新、路径求和、子树节点更新、子树求和)模板-备注结合一下以前写的题目,懒得写很详细的注释

    P3384 [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节 ...

随机推荐

  1. Nmap小技巧——探测大网络空间中的存活主机

    Nmap快速探测空间主机是否存活的技巧(来自lijiejie师傅): nmap -v -sn -PE -n --min-hostgroup --min-parallelism -oX nmap_out ...

  2. Auto Layout Masonry

    1. Auto layout 1.1 NSLayoutConstraint 1.1.1 约束类 ios6.0可用 为了更好的适配各个尺寸 1.1.2 constraintWithItem:attrib ...

  3. LOJ2083 [NOI2016] 优秀的拆分 【哈希】【调和级数】

    题目分析: 好题!我们发现题目实际是要求出从某个左端点开始跑出去的BB型有多少个和从某个右端点开始跑出去的AA型有多少个. 发现这个问题是对称的,所以只考虑从左端点跑出去的BB型有多少个就可以了. 我 ...

  4. [GoogleBlog]new-approach-to-china

    https://googleblog.blogspot.com/2010/01/new-approach-to-china.html

  5. EnableEurekaServer基本配置

    pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  6. spring 中的设计模式

    https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=2247485205&idx=1&sn=63455d2313776d ...

  7. Django 中的static文件的设置

    STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ('article',os.path.jo ...

  8. 二周工作总结(php方向)

    前言:年后回来在忙着换工作,最终拿到了三家的offer,最后权衡去了一家实业公司做oa系统的开发,刚入职做一些技术的总结同时记录自己的技术进步 (一) 用mysql视图实现多个表之间的联查 优点:在实 ...

  9. ACM-ICPC 2018 沈阳赛区网络预赛 B Call of Accepted(表达式求值)

    https://nanti.jisuanke.com/t/31443 题意 给出一个表达式,求最小值和最大值. 表达式中的运算符只有'+'.'-'.'*'.'d',xdy 表示一个 y 面的骰子 ro ...

  10. Linux进程管理工具Supervisor

    简述 Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启. 它是通过fork/exec的方式把这些被管 ...