解题:USACO10MAR Great Cow Gather
可以水水换根的,不过我是另一种做法:(按点权)找重心,事实上这是重心的一个性质
考虑换根的这个过程,当我们把点放在重心时,我们移动这个点有两种情况:
1.移动到最大的那个子树里
可以发现这个最大的子树的$size$不可能超过其余子树的$size$之和(最多是有两个重心的时候等于其他子树的$size$之和),不然你找的是假重心
2.移动到其他的子树里
显然这样只会让答案变差
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
long long dis[N],val[*N],num[N],maxx[N],siz[N];
int p[N],noww[*N],goal[*N],vis[N];
long long ans,tot,t3,maxn=1e16;
int n,c,t1,t2,cnt;
void link(int f,int t,long long v)
{
noww[++cnt]=p[f],p[f]=cnt;
goal[cnt]=t,val[cnt]=v;
}
void MARK(int nde,int fth)
{
siz[nde]=num[nde];
for(int i=p[nde];i;i=noww[i])
if(goal[i]!=fth)
{
MARK(goal[i],nde);
siz[nde]+=siz[goal[i]];
maxx[nde]=max(maxx[nde],siz[goal[i]]);
}
maxx[nde]=max(maxx[nde],tot-siz[nde]);
if(maxx[nde]<maxn) maxn=maxx[nde],c=nde;
}
void DFS(int nde,int fth)
{
for(int i=p[nde];i;i=noww[i])
if(goal[i]!=fth)
{
dis[goal[i]]=dis[nde]+val[i];
DFS(goal[i],nde);
}
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%lld",&num[i]),tot+=num[i];
for(int i=;i<n;i++)
{
scanf("%d%d%lld",&t1,&t2,&t3);
link(t1,t2,t3),link(t2,t1,t3);
}
c=; MARK(,); DFS(c,);
for(int i=;i<=n;i++)
ans+=dis[i]*num[i];
printf("%lld",ans);
return ;
}
解题:USACO10MAR Great Cow Gather的更多相关文章
- BZOJ 1827 洛谷 2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gather
[题解] 很容易想到暴力做法,枚举每个点,然后对于每个点O(N)遍历整棵树计算答案.这样整个效率是O(N^2)的,显然不行. 我们考虑如果已知当前某个点的答案,如何快速计算它的儿子的答案. 显然选择它 ...
- 洛谷 P2986 [USACO10MAR]Great Cow Gat…(树形dp+容斥原理)
P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat… 题目描述 Bessie is planning the annual Great Cow Gathering for c ...
- 【算法学习笔记】动态规划与数据结构的结合,在树上做DP
前置芝士:Here 本文是基于 OI wiki 上的文章加以修改完成,感谢社区的转载支持和其他方面的支持 树形 DP,即在树上进行的 DP.由于树固有的递归性质,树形 DP 一般都是递归进行的. 基础 ...
- P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…(树规)
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- [USACO10MAR]伟大的奶牛聚集Great Cow Gat…
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- 【题解】Luogu p2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat 树型dp
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- poj3660(Cow Contest)解题报告
Solution: 传递闭包 //if a beats b and b beats c , then a beats c //to cow i, if all the result of conten ...
- P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold 解题报告
P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold 题意 给一个字符串,每次可以从两边中的一边取一个字符,要求取出的字符串字典序最小 可以Hash+二分 也可以S ...
随机推荐
- IDEA配置maven中央库
分两步: STEP :配置maven: STEP :配置IDEA.区分默认配置和项目级配置. STEP 1:maven中央库配置 国内常用的maven库主要是阿里云maven库.华为云maven. 其 ...
- Tree - Information Theory
This will be a series of post about Tree model and relevant ensemble method, including but not limit ...
- 使用FFMPEG 压缩png图片 与tinypng压缩结果对比
Tinypng https://tinypng.com/ 一个在线png压缩工具 FFmpeg https://ffmpeg.org/download.html 原图 903 kb Tinypng压 ...
- AutoResetEvent 方法名称设计缺陷
这个类和方法,让人乍一读是读不明白的.不能通过方法名称明白其含义.所以它的方法名称设计是欠考虑. 应该类似于这样: public static class MyAutoResetEvent { pub ...
- Dingo Api 1.0在laravel5.2中的简单应用
Dingo Api是为基于laravel的开发提供了一系列工具集,这些工具集可以帮助开发者快速构建API.Dingo Api最新的版本是2.0.0-alpha1,这个版本需要php7.0以上的php版 ...
- Python基础_eval(),exec(),globals(),locals(),compile()
转发:http://www.cnblogs.com/yyds/p/6276746.html 1. eval函数 函数的作用: 计算指定表达式的值.也就是说它要执行的Python代码只能是单个运算表达式 ...
- JS中判断对象是不是数组的方法
JavaScript中检测对象的方法 1.typeof操作符 这种方法对于一些常用的类型来说那算是毫无压力,比如Function.String.Number.Undefined等,但是要是检测Arra ...
- PSP Daily新增功能说明书
1.选择输入类别时可以记录原来的输入,支持用户选择记录清空功能 2.添加“恢复最近”button,点击这个按钮可以跳出一个页面显示最近的excel记录,用户可以通过勾选相应的excel文件名,恢复选中 ...
- 【贪心算法】POJ-3190 区间问题
一.题目 Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one wil ...
- 9th 学习博客:使用Codebloks实现C++的图形化界面
使用开发工具codeblocks,添加ResEdit.exe这个控件,可以很方便地进行图形化编辑,这是在网上找得教程,实现的是最基本的在对话框内添加按钮,并实现单击响应在控制台输出相应的文字. mai ...