题目:

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. urllib.urlencode() 无法encode中文, UnicodeEncodeError

    urllib.urlencode() 无法encode中文, UnicodeEncodeError, 具体错误内容如下:File "/System/Library/Frameworks/Py ...

  2. ios推送服务,php服务端

    本文转载至http://my.oschina.net/AStar/blog/176531   生成证书 证书生成参考:https://parse.com/tutorials/ios-push-noti ...

  3. poj2349

    Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 0   Accepted: 0 Descript ...

  4. Django 基于Ajax & form 简单实现文件上传

    前端实现 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="U ...

  5. Nginx启动与停止

    参考:https://www.phusionpassenger.com/library/install/nginx/install/oss/rubygems_rvm/ Starting Nginx Y ...

  6. linux下扩展root分区

    1 查看当前磁盘情况 fdisk -l /dev/sda1 2048 6143 2048 83 Linux /dev/sda2 * 6144 1054719 524288 83 Linux /dev/ ...

  7. Linux 上关于iptables

    有几个命令: 1.service iptables staus   2.service iptables start    3.service iptables restart   有个配置文件/ec ...

  8. Linux电源管理(3)-Generic PM之reboot过程【转】

    本文转载自:http://www.wowotech.net/pm_subsystem/reboot.html 1. 前言 在使用计算机的过程中,关机和重启是最先学会的两个操作.同样,这两个操作在Lin ...

  9. android OTG【转】

    本文转载自:http://blog.csdn.net/xubin341719/article/details/7707056 一.OTG的概念 OTG是On-The-Go的缩写,是近年发展起来的技术, ...

  10. while 读取文件内容

    exec < filename while read line;do echo $line done 方法1 while read line;do echo $line done<$ 方法 ...