HDU5044 Tree(树链剖分)
来源:互联网 发布:快啊晒密软件 编辑:程序博客网 时间:2024/06/14 06:50
Tree
传送门1
传送门2
You are given a tree (an acyclic undirected connected graph) with N nodes. The tree nodes are numbered from 1 to N.
There are N - 1 edges numbered from 1 to N - 1.
Each node has a value and each edge has a value. The initial value is 0.
There are two kind of operation as follows:
● ADD1 u v k: for nodes on the path from u to v, the value of these nodes increase by k.
● ADD2 u v k: for edges on the path from u to v, the value of these edges increase by k.
After finished M operation on the tree, please output the value of each node and edge.
Input
The first line of the input is T (1 ≤ T ≤ 20), which stands for the number of test cases you need to solve.
The first line of each case contains two integers N ,M (1 ≤ N, M ≤10 5),denoting the number of nodes and operations, respectively.
The next N - 1 lines, each lines contains two integers u, v(1 ≤ u, v ≤ N ), denote there is an edge between u,v and its initial value is 0.
For the next M line, contain instructions “ADD1 u v k” or “ADD2 u v k”. (1 ≤ u, v ≤ N, -10 5 ≤ k ≤ 10 5)
Output
For each test case, print a line “Case #t:”(without quotes, t means the index of the test case) at the beginning.
The second line contains N integer which means the value of each node.
The third line contains N - 1 integer which means the value of each edge according to the input order.
Sample Input
2
4 2
1 2
2 3
2 4
ADD1 1 4 1
ADD2 3 4 2
4 2
1 2
2 3
1 4
ADD1 1 4 5
ADD2 3 2 4
Sample Output
Case #1:
1 1 0 1
0 2 2
Case #2:
5 0 0 5
0 4 0
题意
一棵树n个节点,有两种操作
1. 将u到v之间点权加k;
2. 将u到v之间边权加k;
输出修改后的点权和边权。
分析
树链剖分+差分
CODE
#include<cstdio>#include<memory.h>#include<algorithm>#define N 100005using namespace std;struct node { int to,next,id;} edge[N<<1];int head[N],tot=1;int dep[N],sz[N],fa[N],son[N],tot2,top[N];int sgID[N],pos[N];int n;void add(int a,int b,int id) { edge[tot]=(node) {b,head[a],id},head[a]=tot++;}void dfs1(int x) { int k=0; sz[x]=1; dep[x]=dep[fa[x]]+1; for(int i=head[x]; ~i; i=edge[i].next) { int y=edge[i].to; if(y==fa[x])continue; fa[y]=x; pos[y]=edge[i].id; dfs1(y); sz[x]+=sz[y]; if(sz[y]>sz[k])k=y; } if(k)son[x]=k;}void dfs2(int x,int tp) { top[x]=tp; sgID[x]=++tot2; if(son[x])dfs2(son[x],tp); for(int i=head[x]; ~i; i=edge[i].next) { int y=edge[i].to; if(y==fa[x]||y==son[x])continue; dfs2(y,y); }}long long sumedge[N],sumpoint[N];void updatepoint(int x,int y,int k) { while(top[x]!=top[y]) { if(dep[top[x]]<dep[top[y]]) { sumpoint[sgID[top[y]]]+=k; sumpoint[sgID[y]+1]-=k; y=fa[top[y]]; } else { sumpoint[sgID[top[x]]]+=k; sumpoint[sgID[x]+1]-=k; x=fa[top[x]]; } } if(dep[x]<dep[y]) { sumpoint[sgID[x]]+=k; sumpoint[sgID[y]+1]-=k; } else { sumpoint[sgID[y]]+=k; sumpoint[sgID[x]+1]-=k; }}void updatedge(int x,int y,int k) { while(top[x]!=top[y]) { if(dep[top[x]]<dep[top[y]]) { sumedge[sgID[top[y]]]+=k; sumedge[sgID[y]+1]-=k; y=fa[top[y]]; } else { sumedge[sgID[top[x]]]+=k; sumedge[sgID[x]+1]-=k; x=fa[top[x]]; } } if(dep[x]>dep[y]) { sumedge[sgID[y]+1]+=k; sumedge[sgID[x]+1]-=k; } else { sumedge[sgID[x]+1]+=k; sumedge[sgID[y]+1]-=k; }}long long f[N];int main() { int T,m; scanf("%d",&T); for(int cas=1; cas<=T; cas++) { printf("Case #%d:\n",cas); scanf("%d%d",&n,&m); tot=1; tot2=0; memset(sumpoint,0,sizeof sumpoint); memset(sumedge,0,sizeof sumedge); memset(head,-1,sizeof head); memset(son,0,sizeof son); memset(sz,0,sizeof sz); int x,y; for(int i=1; i<n; ++i) { scanf("%d%d",&x,&y); add(x,y,i); add(y,x,i); } dfs1(1); dfs2(1,0); while(m--) { char ch[10]; int x,y,k; scanf("%s",ch); scanf("%d%d%d",&x,&y,&k); if(ch[3]=='1')updatepoint(x,y,k); else if(x!=y)updatedge(x,y,k); } for(int i=2; i<=n; ++i) { sumedge[i]+=sumedge[i-1]; sumpoint[i]+=sumpoint[i-1]; } for(int i=2; i<=n; ++i)f[pos[i]]=sumedge[sgID[i]]; for(int i=1; i<=n-1; ++i) printf("%lld ",sumpoint[sgID[i]]); printf("%lld\n",sumpoint[sgID[n]]); for(int i=1; i<=n-2; ++i)printf("%lld ",f[i]); if(n-1>0)printf("%lld",f[n-1]); puts(""); } return 0;}
- HDU5044--Tree(树链剖分)
- HDU5044 Tree(树链剖分)
- hdu5044 Tree(树链剖分+差分)
- hdu5044(神姿势+树链剖分)
- 树链剖分+离散+扫描(HDU5044)
- hdu5044 Tree 树链剖分,点剖分,边剖分,非递归版
- hdu3966 or hdu5044 树链剖分
- 2014上海网络预选赛1003(树链剖分)HDU5044
- hdu5044(树链剖分+标记法实现区间更新)
- HDU5044 2014上海网络赛1003 tree
- hdu5044 Tree 上海网络赛1003 LCA+输入输出外挂
- hdu5044(二分)
- HDU5044【LCA+差分】
- 【POJ3237】Tree(树链剖分)
- SPOJ-tree:Query on a tree (树链剖分)
- poj 3237 Tree(树链剖分)
- HDU 5044 - Tree (树链剖分)
- POJ 3723 Tree(树链剖分)
- C# 调整控件Z序
- 66.设计模式笔记-工厂方法模式
- ios调试(lldb)
- 稀疏矩阵存储格式总结+存储效率对比:COO,CSR,DIA,ELL,HYB
- MySQL---索引
- HDU5044 Tree(树链剖分)
- HttpURLConnection连接网页和获取数据的使用实例
- 有关lookrotate相关的数学四元数的知识(更新待定)
- AndroidStudio与Java8
- 查看tree数据的叶子节点
- json
- mysql导入比较大的数据时出现容量不够错误
- vue路由智能加载,起始篇
- Java基础——Java多线程中sleep()、wait()和notify()