[Usaco2010 Mar]gather 奶牛大集会
1827: [Usaco2010 Mar]gather 奶牛大集会
Time Limit: 1 Sec Memory Limit: 64 MB Submit: 1129 Solved: 525 [Submit][Status][Discuss]
Description
Input
Output
Sample Input
1
1
0
0
2
1 3 1
2 3 2
3 4 3
4 5 3
Sample Output
#include <cstdio> char buf[10000000], *ptr = buf - 1; inline int readint(){int n = 0;char ch = *++ptr;while(ch < '0' || ch > '9') ch = *++ptr;while(ch <= '9' && ch >= '0'){n = (n << 1) + (n << 3) + ch - '0';ch = *++ptr;}return n; } typedef long long ll; const int maxn = 100000 + 10; struct Edge{int to, val, next;Edge(){}Edge(int _t, int _v, int _n): to(_t), val(_v), next(_n){} }e[maxn * 2]; int fir[maxn] = {0}, cnt = 0; inline void ins(int u, int v, int w){e[++cnt] = Edge(v, w, fir[u]); fir[u] = cnt;e[++cnt] = Edge(u, w, fir[v]); fir[v] = cnt; } int c[maxn]; ll f[maxn], siz[maxn], ans; void dfs1(int u, int fa){f[u] = 0;siz[u] = c[u];for(int v, i = fir[u]; i; i = e[i].next){v = e[i].to;if(v == fa) continue;dfs1(v, u);f[u] += f[v] + siz[v] * e[i].val;siz[u] += siz[v];} } void dfs2(int u, int fa){for(int v, i = fir[u]; i; i = e[i].next){v = e[i].to;if(v == fa) continue;if(siz[1] - 2 * siz[v] < 0){ans += (siz[1] - 2 * siz[v]) * e[i].val;dfs2(v, u);}} } int main(){fread(buf, sizeof(char), sizeof(buf), stdin);int N = readint();for(int i = 1; i <= N; i++) c[i] = readint();for(int u, v, w, i = 1; i < N; i++){u = readint();v = readint();w = readint();ins(u, v, w);}dfs1(1, 0);ans = f[1];dfs2(1, 0);printf("%lld\n", ans);return 0; }
转载于:https://www.cnblogs.com/ruoruoruo/p/7512378.html
[Usaco2010 Mar]gather 奶牛大集会相关推荐
- BZOJ 1827: [Usaco2010 Mar]gather 奶牛大集会 树形DP
[Usaco2010 Mar]gather 奶牛大集会 Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来举办这次集会.每个奶牛居住在 N(1 ...
- BZOJ 1827: [Usaco2010 Mar]gather 奶牛大集会 树形DP + 带权重心
Description Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来举办这次集会.每个奶牛居住在 N(1<=N<=100,0 ...
- bzoj 1827: [Usaco2010 Mar]gather 奶牛大集会【树形dp】
不能用read会TLE!!不能用read会TLE!!不能用read会TLE!! 一开始以为要维护每个点,线段树写了好长(还T了-- 首先dfs一遍,求出点1为集会地点的答案,处理处val[u]为以1为 ...
- bzoj 1657: [Usaco2006 Mar]Mooo 奶牛的歌声(单调栈)
1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 848 Solved: 594 [Sub ...
- BZOJ——T 2097: [Usaco2010 Dec]Exercise 奶牛健美操
http://www.lydsy.com/JudgeOnline/problem.php?id=2097 Time Limit: 10 Sec Memory Limit: 64 MB Submit: ...
- COGS——T 803. [USACO Hol10] 政党 || 1776: [Usaco2010 Hol]cowpol 奶牛政坛
http://www.lydsy.com/JudgeOnline/problem.php?id=1776||http://cogs.pro/cogs/problem/problem.php?pid=8 ...
- [Usaco2010 Hol]cowpol 奶牛政坛
题目描述: 农夫约翰的奶牛住在N (2 <= N <= 200,000)片不同的草地上,标号为1到N.恰好有N-1条单位长度的双向道路,用各种各样的方法连接这些草地.而且从每片草地出发都可 ...
- [Usaco2010 Hol]cowpol 奶牛政坛(入门oj Problem 5274)
第三次发题解哈.有什么表述错误的请见谅(可以提出). 题目源地址:https://www.lydsy.com/JudgeOnline/problem.php?id=1776 文章目录 题目描述 输入 ...
- rust种的南瓜为什么老是消失_农村大集会消失吗?为什么农村大集这么受欢迎?说说我的真实感受...
从小我就喜欢赶大集,记得小时侯逢集,和自己的小伙伴儿拿三五块钱去赶集,花五毛钱就能买买串糖葫芦,再买点香蕉苹果,再秤一点瓜子,逛一逛看一看,这就是快乐的一天. 长大了还是爱赶集,老公总是埋怨我赶集时间 ...
最新文章
- ping不通是不是就真不通?
- 特征图注意力_向往的GAT(图注意力模型)
- 【转】两种方法教你在Ubuntu下轻松关闭触摸板(TinkPad)
- Java中的一些零星容易被忽略的API(及时补充)
- phpcmsV9 数据库配置文件(查找、修改)
- pyqt5在mac os 安装
- 破解zip,WinRaR等压缩包加密
- 华为android手机root,华为手机怎么root
- 如何通过个人博客赚钱,走上财务自由?
- Source Insight 4.0安装破解及简单使用
- 腾讯云默认操作系统被安装的软件
- Xilinx软件开发:PMU实验
- 整理下OSS方面的资料,免得到处找,linux音频编程,open sound system
- 数据库与MPP数仓(十四):招标采购系统的数据仓库构建
- 无法启动服务,错误1053
- vue 二维码解码器
- 通信达股票交易接口-Java语言
- 每次需要输入 Enter passphrase for key '/c/Users/Administrator/.ssh/id_rsa'
- ENVI下的Landsat8图像融合
- 微信公众号引流源码?基本上没啥用!