解题: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 ...
随机推荐
- web07-jdbcBookStore
新建web项目,名字 新建servlet,名字CreateDBServlet 内容为: ---- 配置web.xml 数据库的URL.driveclass.user.passWord都写在web.xm ...
- 2018软工实践—Beta冲刺(2)
队名 火箭少男100 组长博客 林燊大哥 作业博客 Beta 冲鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调组内工作 修改前端界面 展示GitHub当日代码/文档签入记录(组内 ...
- week3a:个人博客作业
1.博客上的问题 阅读下面程序,请回答如下问题: using System; using System.Collections.Generic; using System.Text; namespac ...
- React---点击按钮实现内容复制功能
思路: 1.给要复制的内容容器添加一个标签(可以是ID,可以是类名等),通过dom技术获取该容器对象: 2.创建Range对象(某个区域内连续的内容),把该容器对象放进去: 3.将Range对象添加到 ...
- 用css 实现凹陷的线条
box-shadow: 0 1px 0 rgba(255,255,255,0.2) inset,0 -1px 0 rgba(0,0,0,.2) inset; 因为颜色为透明颜色,所以颜色是什么样的,不 ...
- yum与rmp
清理一切缓存[root@geust02 ~]# yum clean all 重建元数据[root@geust02 ~]# yum makecache 查询vim相关的软件包[root@geu ...
- 蜗牛慢慢爬 LeetCode 15. 3Sum [Difficulty: Medium]
题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...
- 9th 学习博客:使用Codebloks实现C++的图形化界面
使用开发工具codeblocks,添加ResEdit.exe这个控件,可以很方便地进行图形化编辑,这是在网上找得教程,实现的是最基本的在对话框内添加按钮,并实现单击响应在控制台输出相应的文字. mai ...
- Jenkins权限控制-Role Strategy Plugin插件使用
Role Strategy Plugin插件可以对构建的项目进行授权管理,让不同的用户管理不同的项目,将测试和生产环境分开. 具体配置方法如下(操作需要管理员用户权限). Jenkins版本:1.64 ...
- PAT 甲级 1141 PAT Ranking of Institutions
https://pintia.cn/problem-sets/994805342720868352/problems/994805344222429184 After each PAT, the PA ...