【codeforces 348B】Apple Tree
【题目链接】:http://codeforces.com/problemset/problem/348/B
【题意】
给你一棵树;
叶子节点有权值;
对于非叶子节点;
它的权值是以这个节点为根的子树上的叶子节点的权值的和;
定义一棵树是平衡的,当且仅当,每个节点的所有直系儿子的权值都相等;
问你要使得这棵树平衡,最少需要删除掉多少叶子节点上的权值;
【题解】
在第一个dfs里面求出d[i]和s[i];
设d[i]表示以i为根的子树要平衡的话最少需要多少权值(注意这里不是说最少要删去多少权值,而是最少需要多少权值),对于叶子节点d[i]=1,非叶子节点的话,d[i] = k*lcm(d[j]),j是i的儿子节点,k是i的直系儿子节点个数;
因为只有按照这样的分法,才能保证都能平均地分下来;也就是说如果最后i节点有权值的话,他一定得是d[i]的倍数;
(lcm是最小公倍数);
s[i]是以i为根的子树里面叶子节点的权值和;
在第二个dfs里面算需要删掉多少;
对于i节点;
先求出t = d[i]/k;这里k同样是i奇点的直系儿子节点的个数;
这样,我们就先算出了,每个儿子节点的权值都应该是t的倍数才对;
然后我们求出所有儿子节点j里面,s的值最小的s[j]=mis;
然后让所有的s[j]变成最小的t的倍数,也即t = (mis/t) * t
然后∑s[j] - t*k 就是需要扣除掉的;
注意这里之后要更新每个儿子节点的s值,同时更新i节点的s值;
注意为根节点的时候,len和非和节点的时候的len…,根节点就算len=1也得继续往下做,不能直接退出。。。所以注意叶子节点的判断。
【Number Of WA】
1
【反思】
这道题,注意到每个节点都有一个最小的平衡权值,且注意到都得是这个最小平衡权值的整数倍是关键。
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100;
const LL oo = 1e18;
LL d[N],s[N],a[N],sum,ans;
int n;
vector <int> G[N];
LL lcm(LL x,LL y){
return (x/__gcd(x,y))*y;
}
void out(){
cout << sum << endl;
exit(0);
}
void dfs1(int x,int fa){
d[x] = 1,s[x] = a[x];
if (G[x].size()==1 && x!=1){
return;
}
int len = G[x].size();
d[x] = 1;
rep1(i,0,len-1){
int y = G[x][i];
if (y==fa) continue;
dfs1(y,x);
d[x] = lcm(d[x],d[y]);
if (d[x]>sum) out();
s[x] += s[y];
}
if (x==1)
d[x] = d[x]*len;
else
d[x] = d[x]*(len-1);
}
void dfs2(int x,int fa){
int len = G[x].size();
if (len==1 && x!=1) return;
LL mis = oo,temp = 0;
rep1(i,0,len-1){
int y = G[x][i];
if (y == fa) continue;
dfs2(y,x);
mis = min(mis,s[y]);
temp+=s[y];
}
LL t;
if (x==1)
t = d[x]/len;
else
t = d[x]/(len-1);
t = (mis/t)*t;
if (x==1)
ans += temp-t*len;
else
ans += temp-t*(len-1);
s[x] = a[x];
rep1(i,0,len-1){
int y = G[x][i];
if (y==fa) continue;
s[y] = t;
s[x]+=s[y];
}
}
int main(){
//Open();
Close();
cin >> n;
rep1(i,1,n){
cin >> a[i];
sum += a[i];
}
rep1(i,1,n-1){
int x,y;
cin >> x >> y;
G[x].pb(y);
G[y].pb(x);
}
dfs1(1,-1);
dfs2(1,-1);
cout << ans << endl;
return 0;
}
【codeforces 348B】Apple Tree的更多相关文章
- 【POJ 2486】 Apple Tree(树型dp)
[POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8981 Acce ...
- 【POJ 2486】 Apple Tree (树形DP)
Apple Tree Description Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to a ...
- 【13.91%】【codeforces 593D】Happy Tree Party
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 723F】st-Spanning Tree
[题目链接]:http://codeforces.com/contest/723/problem/F [题意] 给你一张图; 让你选择n-1条边; 使得这张图成为一颗树(生成树); 同时s的度数不超过 ...
- 【树形dp】Apple Tree
[poj2486]Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10800 Accepted: 3 ...
- 【Codeforces 715C】Digit Tree(点分治)
Description 程序员 ZS 有一棵树,它可以表示为 \(n\) 个顶点的无向连通图,顶点编号从 \(0\) 到 \(n-1\),它们之间有 \(n-1\) 条边.每条边上都有一个非零的数字. ...
- Codeforces 348B:Apple Tree(DFS+LCM+思维)
http://codeforces.com/contest/348/problem/B 题意:给一棵树,每个叶子结点有w[i]个苹果,每个子树的苹果数量为该子树所有叶子结点苹果数量之和,要使得每个结点 ...
- 【POJ 3321】Apple Tree
有n个节点以1为根节点的树,给你树的边关系u-v,一开始每个节点都有一个苹果,接下来有两种操作,C x改变节点x的苹果状态,Q x查询x为根的树的所有苹果个数. 求出树的dfs序,st[i]保存i ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
随机推荐
- IOS - 网络指示器
#pragma mark Activity methods - (void)openActivity { // 添加网络指示器 activityIV = [[UIActivityIndicatorVi ...
- Linux下pyftplib简单的脚本
from pyftpdlib.authorizers import DummyAuthorizer from pyftpdlib.handlers import FTPHandler from pyf ...
- uboot的readme导读
UBOOT的移植其实并没有想象中的难,这主要归功于众多的工程师已经将常见的平台代码写入了UBOOT,我们所要做的就是一点小小的更改,在网上看了很多相关的移植,也听到有人说其实看了UBOOT的readm ...
- vue 根据下拉框动态切换form的rule
taskCategorySelect (val) { // 任务类别下拉选择 if ( val == 5 ) { this.cameraORgateway = false; // true不可以使用 ...
- JavaScript push(),join() 函数
定义和用法 push方法 可向数组的末尾添加一个或多个元素,并返回一个新的长度. join方法 用于把数组中所有元素添加到一个指定的字符串,元素是通过指定的分隔符进行分割的. 语法 arrayObje ...
- lucene构建restful风格的简单搜索引擎服务
来自于本人博客: lucene构建restful风格的简单搜索引擎服务 本人的博客如今也要改成使用lucene进行全文检索的功能,因此在这里把代码贴出来与大家分享 一,文件夹结构: 二,配置文件: 总 ...
- hdu Swipe Bo(bfs+状态压缩)错了多次的题
Swipe Bo Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- Android知识点总结
说明 当中大部分文章都是转载自其它大神之手.在转载的过程中学到了非常多,这里主要解说的是android体系的相关知识点,本文会持续更新. 1 Android service相关知识点 Android ...
- UI_UIImagePickerController(读取图片)
创建图片 #pragma mark - 创建 photoImageView - (void)createphotoImageView { self.photoImageView = [[UIImage ...
- hive 运行sqlclient异常
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. java.lang.Runtim ...