解题: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 ...
随机推荐
- Annotation 使用备忘
title: Annotation 使用备忘 date: 2016-11-16 23:16:43 tags: [Annotation] categories: [Programming,Java] - ...
- 转载----C/C++ 中 const 修饰符用法总结
感谢原创作者,写的好详细.不忍错过,所以转载过来了... 原文地址: https://www.cnblogs.com/icemoon1987/p/3320326.html 在这篇文章中,我总结了一些C ...
- max number of clients reached Redis测试环境报错
现象:测试服务是去redis循环取数据,早上发现服务挂了,手动登陆redis 无法输入命令,报错:max number of clients reached Redis
- Java-URLEncoder.encode 什么时候才是必须的
当你希望把一段 URL 当成另一个 URL 的参数时,比如:当用户点击交易的按钮时你发现未登录就跳转到 login 页面同时带上一个参数记录在登录之前用户是希望访问的那个交易页面,这样在登录完成之后再 ...
- C++ 函数 内联函数
内联函数的功能和预处理宏的功能相似,在介绍内联函数之前,先介绍一下预处理宏.宏是简单字符替换,最常见的用法:定义了一个代表某个值的全局符号.定义可调用带参数的宏.作为一种约定,习惯上总是用大写字母来定 ...
- Thunder-Final冲刺中间产物-2017秋-软件工程第十二次作业
Thunder-Final发布中间产物(WBS&PSP) WBS: 分解方式:按照「爱阅」阅读器的实施过程分解 使用工具:visio 2013 PSP: PSP 预期时间 Planning 计 ...
- 冲刺One之站立会议2
在确定了总体目标之后,我们先决定了实现的具体功能,包括一个登陆界面,一个聊天室的主界面和服务器端的内容.我们今天完成了一小部分内容,把每个内容的主体框架搭建了起来. 效果如下图所示: 燃尽图2
- 深入理解mybatis
MyBatis是目前非常流行的ORM框架,它的功能很强大,然而其实现却比较简单.优雅.本文主要讲述MyBatis的架构设计思路,并且讨论MyBatis的几个核心部件,然后结合一个select查询实例, ...
- SDPA: Toward a Stateful Data Plane in Software-Defined Networking
文章名称:SDPA: Toward a Stateful Data Plane in Software-Defined Networking 发表时间:2017 期刊来源:IEEE/ACM Trans ...
- Scapy 网段中ping扫描
安装scapy pip3 install scapy-python3 交互式ip包构造 #scapy >>> ping = sr(IP(dst='202.100.1.1')/ICMP ...