【题目链接】: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的更多相关文章

  1. 【POJ 2486】 Apple Tree(树型dp)

    [POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8981   Acce ...

  2. 【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 ...

  3. 【13.91%】【codeforces 593D】Happy Tree Party

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【codeforces 723F】st-Spanning Tree

    [题目链接]:http://codeforces.com/contest/723/problem/F [题意] 给你一张图; 让你选择n-1条边; 使得这张图成为一颗树(生成树); 同时s的度数不超过 ...

  5. 【树形dp】Apple Tree

    [poj2486]Apple Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10800   Accepted: 3 ...

  6. 【Codeforces 715C】Digit Tree(点分治)

    Description 程序员 ZS 有一棵树,它可以表示为 \(n\) 个顶点的无向连通图,顶点编号从 \(0\) 到 \(n-1\),它们之间有 \(n-1\) 条边.每条边上都有一个非零的数字. ...

  7. Codeforces 348B:Apple Tree(DFS+LCM+思维)

    http://codeforces.com/contest/348/problem/B 题意:给一棵树,每个叶子结点有w[i]个苹果,每个子树的苹果数量为该子树所有叶子结点苹果数量之和,要使得每个结点 ...

  8. 【POJ 3321】Apple Tree

    有n个节点以1为根节点的树,给你树的边关系u-v,一开始每个节点都有一个苹果,接下来有两种操作,C x改变节点x的苹果状态,Q x查询x为根的树的所有苹果个数.   求出树的dfs序,st[i]保存i ...

  9. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

随机推荐

  1. ansible yum 模块 安装 vsftp

    [root@ftp:/root] > ansible ansible01 -u root -k -m yum -a 'name=vsftpd state=latest' SSH password ...

  2. PHP中的 Iterator 与 Generator

    在讲解生成器之前先介绍一下迭代器: 在 PHP 中,通常情况下遍历数组使用 foreach 来遍历. 如果我们要想让一个对象可以遍历呢? PHP 为我们提供了 Iterator 接口,只要实现了这个接 ...

  3. python 进程 multiprocessing模块

    一.multiprocess.process模块 1.process类 Process([group [, target [, name [, args [, kwargs]]]]]),由该类实例化得 ...

  4. apche本地测试,无法访问此网站

  5. WinServer-IIS-FTP服务器

    在设置FTP服务器的时候,注意SSL的设置,简单尝试的话,不需要启用SSL 常用FTP命令 ftp   进入FTP命令模式 open 192.168.10.6     (连接到FTP主机,或者open ...

  6. C#-WebService基础02

    WebService WSDL是web service的交换格式 跨平台数据交互 什么是web服务 SOA 面向服务的体系结构  service-Oriented Architecture Servi ...

  7. ssm框架下上传图片及其他信息

    先引入这两个包: <dependency> <groupId>commons-fileupload</groupId> <artifactId>comm ...

  8. python设计模式 之 简单工厂模式

    简单工厂模式属于类的创建型模式,适合用来对大量具有共同接口的类进行实例化,它能够推迟到执行的时候才动态决定要创建哪个类的实例.而不是在编译时就必须知道要实例化哪个类. python: #!/usr/b ...

  9. 1067. Sort with Swap(0,*) (25)【贪心】——PAT (Advanced Level) Practise

    题目信息 1067. Sort with Swap(0,*) (25) 时间限制150 ms 内存限制65536 kB 代码长度限制16000 B Given any permutation of t ...

  10. Process Explorer

    https://docs.microsoft.com/en-us/sysinternals/downloads/process-explorer Ever wondered which program ...