51nod 1405 树的距离之和(树型dp)
来源:互联网 发布:淘宝钻展在哪里展示 编辑:程序博客网 时间:2024/04/28 04:53
51nod 1405
题目
中文题目
思路
考虑还是不能一个个点去思考,而是看边与所求的关系,还是菜菜的。
num[i]记录i有多少个子节点,dp[i]表示所有点到i的距离和,dep[i]表示i的深度,结点u,对于它的子节点x,x的子节点到x的距离比到u的距离少1,除了x,u以外的点到x的距离比到u的距离多一,所以dp[x]=dp[u]-num[x]-(n-2-num[x]),根节点根据深度就可以求出,然后就可以求出所有。
代码
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <cmath>#include <iostream>using namespace std ;typedef long long ll;const int maxn=100010;int tot,head[maxn],n;struct node{ int next; int to;} edge[maxn*2];void addedge(int from,int to){ edge[tot].to=to; edge[tot].next=head[from]; head[from]=tot++;}int num[maxn],dep[maxn];ll dp[maxn],tol;void dfs1(int u,int fa,int depth){ num[u]=0; dep[u]=depth; tol+=dep[u]; for(int i=head[u]; ~i; i=edge[i].next) { int v=edge[i].to; if(v==fa) continue; dfs1(v,u,depth+1); num[u]+=num[v]+1; }}void dfs2(int u,int fa){ for(int i=head[u]; ~i; i=edge[i].next) { int v=edge[i].to; if(v==fa) continue; dp[v]=dp[u]+n-2-2*num[v]; dfs2(v,u); }}int main(){ scanf("%d",&n); tot=0; tol=0; memset(head,-1,sizeof(head)); for(int i=1; i<n; i++) { int a,b; scanf("%d %d",&a,&b); addedge(a,b); addedge(b,a); } dfs1(1,-1,0); dp[1]=tol; dfs2(1,-1); for(int i=1; i<=n; i++) printf("%I64d\n",dp[i]); return 0 ;}
0 0
- 51nod 1405 树的距离之和(树型dp)
- 51nod 1405 树的距离之和【树型dp】
- 51Nod 1405 树的距离之和(树形dp)
- 51NOD-1405 树的距离之和(树形DP)
- 51nod 1405 树的距离之和(DP)
- 51nod 1405 树的距离之和 (树形dp)
- 51Nod 1405 树的距离之和(dp)
- 51nod 1405 树的距离之和【树形dp】
- 51nod 1405 树的距离之和 搜索+DP
- 51 nod 1405 树的距离之和 详细题解(树形DP)
- 51nod 1405 树的距离之和
- 51nod 1405 树的距离之和
- 51nod-1405 树的距离之和
- 51nod 1405 树的距离之和
- 51nod 1405 树的距离之和
- 51nod 1405 树的距离之和
- 51nod 1405 树的距离之和
- 51nod 1405 树的距离之和 【dfs--记忆dp??树形dp??】
- Mac OS X新建文本文件,txt文件
- 辗转相除法
- 在unity中用鼠标实现在场景中拖动物体,用鼠标滚轮实现缩放
- [Leetcode] Counting Bits
- 二叉树中序遍历成双向链表
- 51nod 1405 树的距离之和(树型dp)
- 二叉树的深度优先遍历与广度优先遍历 [ C++ 实现 ]
- java中基本数据类型的初始化默认值
- 1093. Count PAT's (25)
- 安卓入门-UI 之自定义actionbar样式
- 使用BeautifulSoup爬取github内容示例
- <boost-03>如何使用boost filesystem获取给定子文件夹的目录?how to use boost filesystem to get subfolders?
- 自动化生成 Openstack 新项目开发框架
- requestAnimationFrame的动画循环