题目:

Description

吉丽YY了一道神题,题面是这样的:

“一棵n个点的树,每条边长度为1,第i个结点居住着a[i]个人。假设在i结点举行会议,所有人都从原住址沿着最短路径来到i结点,行走的总路程为b[i]。输出所有b[i]。”

吉丽已经造好了数据,但熊孩子把输入文件中所有a[i]给删掉了。你能帮他恢复吗?

题解:

对于节点\(u\)设其父亲为\(fa_u\).子树的\(a_i\)之和为\(sum_i\)

设\(SUM = \sum_{u \in G}a_u\)

则对于任意的\(u \neq 1\)有:\(b_u - b_{fa_u} = SUM - 2*sum_u\)

且\(b_1 = \sum_{u \neq 1}sum_u\)

然后我们将所有的\(b_u - b_{fa_u} = SUM - 2*sum_u\)求和

得:\(\sum (b_u - b_{fa_u}) = (n-1)*SUM - 2\sum_{u \neq 1}sum_u\)

我们将\(b_1 = \sum_{u \neq 1}sum_u\)翻倍加上去有.

\(2*b_1 = \sum (b_u - b_{fa_u}) = (n-1)*SUM\)

于是我们有:\(SUM = \frac{2*b_1 + \sum_{u \neq 1}(b_u - b_{fa_u})}{n-1}\)

既然我们得到了SUM,那么将其代入到所有的\(b_u - b_{fa_u} = SUM - 2*sum_u\)中即可

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
const int maxn = 300010;
int n;
struct Egde{
int to,next;
}G[maxn<<1];
int head[maxn],cnt;
void add(int u,int v){
G[++cnt].to = v;
G[cnt].next = head[u];
head[u] = cnt;
}
int b[maxn],sum[maxn],fa[maxn],a[maxn];
#define v G[i].to
void dfs1(int u){
for(int i = head[u];i;i=G[i].next){
if(v == fa[u]) continue;
fa[v] = u;dfs1(v);
}
}
void dfs2(int u){
a[u] = sum[u];
for(int i = head[u];i;i=G[i].next){
if(v == fa[u]) continue;
dfs2(v);a[u] -= sum[v];
}
}
#undef v
inline void init(){
memset(head,0,sizeof head);
cnt = 0;
}
int main(){
init();read(n);
for(int i=1,u,v;i<n;++i){
read(u);read(v);
add(u,v);add(v,u);
}
memset(sum,0,sizeof sum);
memset(fa,0,sizeof fa);
for(int i=1;i<=n;++i) read(b[i]);
dfs1(1);
ll x = 0;
for(int i=2;i<=n;++i){
x += b[i] - b[fa[i]];
}x += 2LL*b[1];
ll SUM = x/(n-1);
sum[1] = SUM;
for(int i=2;i<=n;++i){
sum[i] = SUM - (b[i] - b[fa[i]]);
sum[i] >>= 1;
}
dfs2(1);
for(int i=1;i<=n;++i){
printf("%d",a[i]);
if(i != n) putchar(' ');
else putchar('\n');
}
return 0;
}

bzoj 3727: Final Zadanie 思维题的更多相关文章

  1. 【BZOJ 3727】 3727: PA2014 Final Zadanie (递推)

    3727: PA2014 Final Zadanie Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 279  Solved: 121 Descript ...

  2. HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)

    HDU 1029 Ignatius and the Princess IV (思维题,排序?) Description "OK, you are not too bad, em... But ...

  3. 计蒜客 28319.Interesting Integers-类似斐波那契数列-递推思维题 (Benelux Algorithm Programming Contest 2014 Final ACM-ICPC Asia Training League 暑假第一阶段第二场 I)

    I. Interesting Integers 传送门 应该是叫思维题吧,反正敲一下脑壳才知道自己哪里写错了.要敢于暴力. 这个题的题意就是给你一个数,让你逆推出递推的最开始的两个数(假设一开始的两个 ...

  4. BZOJ 2734 洛谷 3226 [HNOI2012]集合选数【状压DP】【思维题】

    [题解] 思维题,看了别人的博客才会写. 写出这样的矩阵: 1,3,9,... 2,6,18,... 4,12.36,... 8,24,72,... 我们要做的就是从矩阵中选出一些数字,但是不能选相邻 ...

  5. UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)

    UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...

  6. 【BZOJ3727】PA2014 Final Zadanie 树形DP

    [BZOJ3727]PA2014 Final Zadanie Description 吉丽YY了一道神题,题面是这样的:“一棵n个点的树,每条边长度为1,第i个结点居住着a[i]个人.假设在i结点举行 ...

  7. CodeForces - 631C ——(思维题)

    Each month Blake gets the report containing main economic indicators of the company "Blake Tech ...

  8. zoj 3778 Talented Chef(思维题)

    题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...

  9. cf A. Inna and Pink Pony(思维题)

    题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...

随机推荐

  1. 利用solr6.5,tomcat9.0和centos7.0的搭建

    第一步:去官网下载所需的软件包, jdk1.8   wget http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff ...

  2. Objective-C 和 Core Foundation 对象相互转换的内存管理总结

    本文转载至 http://blog.csdn.net/allison162004/article/details/38756649 OS允许Objective-C 和 Core Foundation ...

  3. 记CBS一次动人心魄的数据保卫战

    接触分布式存储已经有一年多的时间了,首次遇到存储侧三份数据都有异常的情况,三份数据异常意味着客户数据的丢失,这个对云存储来讲是致命的打击.为了保证数据的安全,CBS运维和开发的同学进行了持续两天一夜的 ...

  4. 第一课 第一个nodejs程序

    这就是我们的第一个程序了 在控制台会输出:hello 我们需要运行该文件 开始->运行 cmd 进入我们的程序目录 我的是D:/nodejs/hello.js 进入程序目录cd D:/nodej ...

  5. POJ 2993 Emag eht htiw Em Pleh【模拟画棋盘】

    链接: http://poj.org/problem?id=2993 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27454#probl ...

  6. Android系统字体规范

    我们在做Android移动APP设计的时候,字号的选择也是很让人头疼,转载一份有关Android系统字体规范,如果在做Android项目的用户应该看看,如果有任何建议欢迎在留言处与我们交流探讨. 主要 ...

  7. 解压tar包中的指定文件

    解压<a 'tar');"="" href="http://asmboy001.blog.51cto.com/'#\'"" targe ...

  8. LRC歌词文件读取代码

    /**************************************************/ /*******************-main文件-******************* ...

  9. FreeMarker使用后台枚举

    //页面使用枚举全路径访问 model.addAttribute("enums", BeansWrapper.getDefaultInstance().getEnumModels( ...

  10. 【leetcode刷题笔记】Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...