51 nod 1405 树的距离之和
1405 树的距离之和
第一行包含一个正整数n (n <= 100000),表示节点个数。
后面(n - 1)行,每行两个整数表示树的边。
每行一个整数,第i(i = 1,2,...n)行表示所有节点到第i个点的距离之和。
4
1 2
3 2
4 2
5
3
5
5
/*
51 nod 1405 树的距离之和 problem:
给定一棵无根树,假设它有n个节点,节点编号从1到n, 对于每个i求所有点到i的和。 solve:
假设已经知道了所有点到u的和, 对于它右儿子v的和,可以发现增加了Size[左子树]条uv边.减少了Size[v]条uv边
所以先求出所有点到1的和,然后可以推出其它所有点 hhh-2016/09/13-20:15:59
*/
#pragma comment(linker,"/STACK:124000000,124000000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <math.h>
#include <queue>
#include <set>
#include <map>
#define lson i<<1
#define rson i<<1|1
#define ll long long
#define clr(a,b) memset(a,b,sizeof(a))
#define scanfi(a) scanf("%d",&a)
#define scanfs(a) scanf("%s",a)
#define scanfl(a) scanf("%I64d",&a)
#define scanfd(a) scanf("%lf",&a)
#define key_val ch[ch[root][1]][0]
#define eps 1e-7
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
const int maxn = 100110; template<class T> void read(T&num)
{
char CH;
bool F=false;
for(CH=getchar(); CH<'0'||CH>'9'; F= CH=='-',CH=getchar());
for(num=0; CH>='0'&&CH<='9'; num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p)
{
if(!p)
{
puts("0");
return;
}
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} ll ans[maxn];
struct node
{
int to,next;
}edge[maxn <<2]; int tot,n;
int head[maxn];
int Size[maxn];
ll f[maxn];
void add_edge(int u,int v)
{
edge[tot].to = v,edge[tot].next = head[u],head[u] = tot ++;
} void dfs(int u,ll len,int pre)
{
f[u] = len;
Size[u] = 1;
for(int i = head[u]; i != -1;i = edge[i].next )
{
int v = edge[i].to;
if(v == pre)
continue;
dfs(v,len+1,u);
Size[u] += Size[v];
}
} void solve(int u,ll tans,int pre)
{
ans[u] = tans;
for(int i = head[u];i != -1;i = edge[i].next)
{
int v = edge[i].to;
if(v == pre)
continue;
ll t = n-Size[v]*2;
solve(v, (ll)tans + t,u);
}
} void init()
{
tot = 0;
clr(head,-1);
} int main()
{
int u,v;
while(scanfi(n) != EOF)
{
init();
for(int i = 1;i < n;i++)
{
scanfi(u),scanfi(v);
add_edge(u,v);
add_edge(v,u);
}
dfs(1,0,-1);
ll ta = 0;
for(int i =1;i <= n;i++)
ta += f[i];
solve(1,ta,-1);
for(int i = 1;i <= n;i++)
{
print(ans[i]);
}
}
}
51 nod 1405 树的距离之和的更多相关文章
- 51nod 1405 树的距离之和 树形dp
1405 树的距离之和 基准时间限制:1 秒 空间限制:131072 KB 收藏 关注 给定一棵无根树,假设它有n个节点,节点编号从1到n, 求任意两点之间的距离(最短路径)之和. Input ...
- 51Nod - 1405 树的距离之和(树形DP)
1405 树的距离之和 题意 给定一棵无根树,假设它有n个节点,节点编号从1到n,求任意两点之间的距离(最短路径)之和. 分析 树形DP. 首先我们让 \(1\) 为根.要开两个数组 \(up \ d ...
- 51Nod 1405 树的距离之和(dp)
1405 树的距离之和 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 给定一棵无根树,如果它有n个节点,节点编号从1到n, 求随意两点之间的距离( ...
- 51nod 1405 树的距离之和(dfs)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1405 题意: 思路: 先求出所有点到根节点的距离,需要维护每棵子树的大小 ...
- 51Nod 1405 树的距离之和 (树dp)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1405 中文题面不解释了,两次dfs,第一次自下向上,第二次自上 ...
- [51NOD1405] 树的距离之和(树DP)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1405 (1)我们给树规定一个根.假设所有节点编号是0-(n-1 ...
- 51 NOD 1239 欧拉函数之和(杜教筛)
1239 欧拉函数之和 基准时间限制:3 秒 空间限制:131072 KB 分值: 320 难度:7级算法题 收藏 关注 对正整数n,欧拉函数是小于或等于n的数中与n互质的数的数目.此函数以其首名研究 ...
- 51 Nod 1110距离之和最小V3
1110 距离之和最小 V3 1 秒 131,072 KB 40 分 4 级题 X轴上有N个点,每个点除了包括一个位置数据X[i],还包括一个权值W[i].点P到点P[i]的带权距离 = 实际距离 * ...
- 51 nod 1766 树上的最远点对(线段树+lca)
1766 树上的最远点对 基准时间限制:3 秒 空间限制:524288 KB 分值: 80 难度:5级算法题 n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间,表示点的标号请你求出两个 ...
随机推荐
- 关于python中的operator.itemgetter()函数的用法
1. operator.itemgetter(num)函数 表示对对象的第num维数据进行操作获取. >>>import operator >>>a = [1, 2 ...
- Beta冲刺计划---Day0
Beta阶段报告---Day0 1.需要改进完善的功能 我们上一阶段开发由于开发时间匆忙,对于爬虫耗时的优化没有考虑.优化的空间我在Alpha阶段的总结报告里说过,具体看下图. 这张图显示出爱 ...
- 网页设计入门<一>
俗话说:技多不压身.实习周,网页设计是之一,边学边总结... 本次网页设计基于Adobe Dreamweaver CS6开发平台,根据实习老师的暴力指导,为什么说暴力呢? 没有基础,没有预告,打开软件 ...
- TSP-旅行商问题
#include <iostream> #include <vector> #include <algorithm> using namespace std; in ...
- LR回放https协议脚本失败: 错误 -27778: 在尝试与主机“www.baidu.com”connect 时发生 SSL 协议错误
今天用LR录制脚本协议为https协议,回放脚本时出现报错: Action.c(14): 错误 -27778: 在尝试与主机"www.baidu.com"connect 时发生 S ...
- Gson序列化对象如何忽略字段
Gson序列化对象如何忽略字段 Gson版本 2.8.2 梗概 用注解@Expose(serialize = false, deserialize = false)在类的成员上以告诉Gson 跳过本字 ...
- python全栈开发-常用模块的一些应用
一.random模块详解 1.概述 首先我们看到这个单词是随机的意思,他在python中的主要用于一些随机数,或者需要写一些随机数的代码,下面我们就来整理他的一些用法 2.常用方法 1. random ...
- ### Cause: org.apache.ibatis.binding.BindingException: Parameter 'name' not found. Available parameters are [arg1, arg0, param1, param2]
org.apache.ibatis.exceptions.PersistenceException: ### Error updating database. Cause: org.apache.ib ...
- cv2.cornerHarris()详解 python+OpenCV 中的 Harris 角点检测
参考文献----------OpenCV-Python-Toturial-中文版.pdf 参考博客----------http://www.bubuko.com/infodetail-2498014. ...
- less规范
Less 编码规范 (1.1) 简介 该文档主要的设计目标是提高 Less 文档的团队一致性与可维护性. Less 代码的基本规范和原则与 CSS 编码规范 保持一致. 编撰 吕俊涛 本文档由商业运营 ...