树链剖分——边权poj2763
边权操作起来也和点权一样,只要把边的权值映射到点上即可,要注意的地方是向上爬的过程中和点权不太一样,还有个特判(WA了几次。。)
完整代码
- #include<cstring>
- #include<cstdio>
- #include<iostream>
- using namespace std;
- #define maxn 100005
- struct E{int x,y;}e[maxn];
- struct Edge{int to,nxt,w;}edge[maxn<<];
- int head[maxn],tot,v[maxn],n,q,c;
- void init(){memset(head,-,sizeof head);tot=;}
- void addedge(int u,int v,int w){
- edge[tot].to=v;edge[tot].w=w;edge[tot].nxt=head[u];head[u]=tot++;
- }
- int f[maxn],son[maxn],d[maxn],size[maxn];
- void dfs1(int x,int pre,int deep){
- size[x]=,d[x]=deep,f[x]=pre;
- for(int i=head[x];i!=-;i=edge[i].nxt){
- int y=edge[i].to;
- if(y==pre)continue;
- v[y]=edge[i].w;//注意这里将边权映射到点上
- dfs1(y,x,deep+);
- size[x]+=size[y];
- if(size[y]>size[son[x]])son[x]=y;
- }
- }
- int cnt,top[maxn],id[maxn],rk[maxn];
- void dfs2(int x,int tp){
- top[x]=tp;id[x]=++cnt;rk[cnt]=x;
- if(son[x])dfs2(son[x],tp);
- for(int i=head[x];i!=-;i=edge[i].nxt){
- int y=edge[i].to;
- if(y!=f[x] && y!=son[x])dfs2(y,y);
- }
- }
- #define lson l,m,rt<<1
- #define rson m+1,r,rt<<1|1
- int sum[maxn<<];
- void pushup(int rt){sum[rt]=sum[rt<<]+sum[rt<<|];}
- void build(int l,int r,int rt){
- if(l==r){sum[rt]=v[rk[l]];return;}
- int m=l+r>>;
- build(lson);build(rson);
- pushup(rt);
- }
- void update(int pos,int l,int r,int rt,int v){
- if(l==r){sum[rt]=v;return;}
- int m=l+r>>;
- if(pos<=m)update(pos,lson,v);
- else update(pos,rson,v);
- pushup(rt);
- }
- int query(int L,int R,int l,int r,int rt){
- if(L<=l && R>=r)return sum[rt];
- int m=l+r>>,res=;
- if(L<=m)res+=query(L,R,lson);
- if(R>m)res+=query(L,R,rson);
- return res;
- }
- int Query(int x,int y){
- int res=;
- while(top[x]!=top[y]){
- if(d[top[x]]<d[top[y]])swap(x,y);
- res+=query(id[top[x]],id[x],,n,);
- x=f[top[x]];
- }
- if(x==y)return res;
- if(id[x]>id[y])swap(x,y);
- return res+query(id[son[x]],id[y],,n,);
- }
- int main(){
- init();int s;
- scanf("%d%d%d",&n,&q,&s);
- for(int i=;i<n;i++){
- int x,y,z;scanf("%d%d%d",&x,&y,&z);
- e[i].x=x;e[i].y=y;
- addedge(x,y,z);addedge(y,x,z);
- }
- cnt=;dfs1(s,,);dfs2(s,s);
- build(,n,);
- while(q--){
- int op,x,y;
- scanf("%d",&op);
- if(op==){scanf("%d",&x);
- cout<<Query(s,x)<<'\n';s=x;}
- if(op==){
- scanf("%d%d",&x,&y);
- int u=e[x].x,v=e[x].y;
- if(d[u]<d[v])swap(u,v);
- update(id[u],,n,,y);
- }
- }
- }
不同之处:最后一次查询的时候不要最高的那个点权
特判:x==y的时候直接返回即可
- int Query(int x,int y){
- int res=;
- while(top[x]!=top[y]){
- if(d[top[x]]<d[top[y]])swap(x,y);
- res+=query(id[top[x]],id[x],,n,);
- x=f[top[x]];
- }
- if(x==y)return res;
- if(id[x]>id[y])swap(x,y);
- return res+query(id[son[x]],id[y],,n,);
- }
树链剖分——边权poj2763的更多相关文章
- POJ2763 Housewife Wind 树链剖分 边权
POJ2763 Housewife Wind 树链剖分 边权 传送门:http://poj.org/problem?id=2763 题意: n个点的,n-1条边,有边权 修改单边边权 询问 输出 当前 ...
- BZOJ 1036 [ZJOI2008]树的统计Count (树链剖分 - 点权剖分 - 单点权修改)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1036 树链剖分模版题,打的时候注意点就行.做这题的时候,真的傻了,单词拼错检查了一个多小时 ...
- 计蒜客 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 ...
- POJ3237 Tree 树链剖分 边权
POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...
- HDU3669 Aragorn's Story 树链剖分 点权
HDU3669 Aragorn's Story 树链剖分 点权 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: n个点的,m条边,每个点都 ...
- BZOJ 1984: 月下“毛景树” [树链剖分 边权]
1984: 月下“毛景树” Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 1728 Solved: 531[Submit][Status][Discu ...
- SPOJ 375 (树链剖分 - 边权剖分 - 修改单边权)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28982#problem/I 给你一棵有边权的树,有两个操作:一个操作是输出l到 ...
- 树链剖分边权模板spoj375
树链剖分是树分解成多条链来解决树上两点之间的路径上的问题 如何求出树链:第一次dfs求出树上每个结点的大小和深度和最大的儿子,第二次dfs就能将最大的儿子串起来并hash(映射)到线段树上(或者其他数 ...
- 洛谷 P3384 【模板】树链剖分-树链剖分(点权)(路径节点更新、路径求和、子树节点更新、子树求和)模板-备注结合一下以前写的题目,懒得写很详细的注释
P3384 [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节 ...
随机推荐
- Nmap小技巧——探测大网络空间中的存活主机
Nmap快速探测空间主机是否存活的技巧(来自lijiejie师傅): nmap -v -sn -PE -n --min-hostgroup --min-parallelism -oX nmap_out ...
- Auto Layout Masonry
1. Auto layout 1.1 NSLayoutConstraint 1.1.1 约束类 ios6.0可用 为了更好的适配各个尺寸 1.1.2 constraintWithItem:attrib ...
- LOJ2083 [NOI2016] 优秀的拆分 【哈希】【调和级数】
题目分析: 好题!我们发现题目实际是要求出从某个左端点开始跑出去的BB型有多少个和从某个右端点开始跑出去的AA型有多少个. 发现这个问题是对称的,所以只考虑从左端点跑出去的BB型有多少个就可以了. 我 ...
- [GoogleBlog]new-approach-to-china
https://googleblog.blogspot.com/2010/01/new-approach-to-china.html
- EnableEurekaServer基本配置
pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- spring 中的设计模式
https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=2247485205&idx=1&sn=63455d2313776d ...
- Django 中的static文件的设置
STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ('article',os.path.jo ...
- 二周工作总结(php方向)
前言:年后回来在忙着换工作,最终拿到了三家的offer,最后权衡去了一家实业公司做oa系统的开发,刚入职做一些技术的总结同时记录自己的技术进步 (一) 用mysql视图实现多个表之间的联查 优点:在实 ...
- ACM-ICPC 2018 沈阳赛区网络预赛 B Call of Accepted(表达式求值)
https://nanti.jisuanke.com/t/31443 题意 给出一个表达式,求最小值和最大值. 表达式中的运算符只有'+'.'-'.'*'.'d',xdy 表示一个 y 面的骰子 ro ...
- Linux进程管理工具Supervisor
简述 Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启. 它是通过fork/exec的方式把这些被管 ...