[USACO10MAR]伟大的奶牛聚集Great Cow Gat… ($dfs$,树的遍历)
题目链接
Solution
辣鸡题...因为一个函数名看了我贼久。
思路很简单,可以先随便指定一个根,然后考虑换根的变化。
每一次把根从 \(x\) 换成 \(x\) 的一个子节点 \(y\),记录一下每个节点的子树牛数目 \(son\)。
令 \(sum\) 为所有节点上牛的数目,那么每一次换根变化为 \((sum-son_y*2)*w\)。
然后就可以统计了,复杂度 \(O(n)\) 。
Code
#include<bits/stdc++.h>
#define N 100008
#define ll long long
using namespace std;
void in(ll &x)
{
char ch=getchar();ll f=1,w=0;
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){w=w*10+ch-'0';ch=getchar();}
x=f*w; return;
}
struct sj{
ll to,next,w;
}a[N*2];
ll head[N],size,c[N],n;
void add(ll x,ll y,ll z)
{
a[++size].to=y;
a[size].next=head[x];
head[x]=size;
a[size].w=z;
}
ll sum[N],son[N],ans[N],som[N],Ans,Sum;
void dfs(ll x,ll fr)
{
son[x]=c[x];
for(ll i=head[x];i;i=a[i].next)
{
ll tt=a[i].to;
if(tt==fr)continue;
dfs(tt,x);
son[x]+=son[tt];
sum[x]+=(a[i].w*son[tt]+sum[tt]);
}
}
void work(ll x,ll fr)
{
for(ll i=head[x];i;i=a[i].next)
{
ll tt=a[i].to;
if(tt==fr)continue;
som[tt]=Sum-son[tt];
ans[tt]=ans[x]+som[tt]*a[i].w-son[tt]*a[i].w;
Ans=min(Ans,ans[tt]);
work(tt,x);
}
}
int main()
{
in(n);
for(ll i=1;i<=n;i++) in(c[i]),Sum+=c[i];
for(ll i=1;i<n;i++)
{
ll x,y,w;
in(x),in(y),in(w);
add(x,y,w),add(y,x,w);
}
dfs(1,0); Ans=0x3f3f3f3f3f3f3f;
ans[1]=sum[1]; work(1,0);
cout<<min(ans[1],Ans)<<endl;
return 0;
}
[USACO10MAR]伟大的奶牛聚集Great Cow Gat… ($dfs$,树的遍历)的更多相关文章
- 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 ...
- [洛谷P2986][USACO10MAR]伟大的奶牛聚集Great Cow Gat…
题目大意:给你一棵树,每个点有点权,边有边权,求一个点,使得其他所有点到这个点的距离和最短,输出这个距离 题解:树形$DP$,思路清晰,转移显然 卡点:无 C++ Code: #include < ...
- LUOGU P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…
传送门 解题思路 首先第一遍dfs预处理出每个点的子树的siz,然后可以处理出放在根节点的答案,然后递推可得其他答案,递推方程 sum[u]=sum[x]-(val[i]*siz[u])+(siz[1 ...
- 洛谷 P2986 [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 ...
- [USACO10MAR]伟大的奶牛聚集Great Cow Gat…【树形dp】By cellur925
题目传送门 首先这道题是在树上进行的,然后求最小的不方便程度,比较符合dp的性质,那么我们就可以搞一搞树形dp. 设计状态:f[i]表示以i作为聚集地的最小不方便程度.那么我们还需要各点间的距离,但是 ...
- BZOJ 1827 洛谷 2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gather
[题解] 很容易想到暴力做法,枚举每个点,然后对于每个点O(N)遍历整棵树计算答案.这样整个效率是O(N^2)的,显然不行. 我们考虑如果已知当前某个点的答案,如何快速计算它的儿子的答案. 显然选择它 ...
- 【luoguP2986】[USACO10MAR]伟大的奶牛聚集Great Cow Gathering
题目链接 先把\(1\)作为根求每个子树的\(size\),算出把\(1\)作为集会点的代价,不难发现把集会点移动到\(u\)的儿子\(v\)上后的代价为原代价-\(v\)的\(size\)*边权+( ...
随机推荐
- 007-Spring Boot-@Enable*注解的工作原理-EnableConfigurationProperties、ImportSelector、ImportBeanDefinitionRegistrar
一.@Enable* 启用某个特性的注解 1.EnableConfigurationProperties 回顾属性装配 application.properties中添加 tomcat.host=19 ...
- jmeter逻辑控制器详解(2)
逻辑控制器 8.Runtime Controller 运行周期控制器,顾名思义,这是一种设置运行时间的控制器,它的效果就是使该控制器下的子项运行时间为[Runtime]中的数值(单位:s). Runt ...
- 【报错】springboot thymeleaf超链接跳转 404
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as ...
- python字典-基础
一.解释 像列表一样,“字典”是许多值的集合.但不像列表的下标,字典的索引可以 使用许多不同数据类型,不只是整数.字典的索引被称为“键”,键及其关联的值 称为“键-值”对. 二.列表创建方式 1. I ...
- 剑指Offer编程题(Java实现)——数组中的重复数字
题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...
- fanc委托在项目中使用
一,上代码 using System; namespace FuncDemo { class Program { static void Main(string[] args) { //无参数的fan ...
- EPROCESS ETHREAD PEB重要成员
-----------------------------------------------------------------------win7 x86中-------------------- ...
- jquery 点击加载更多
html部分 <ul class="bill moreadd"> <div class="total"><span>-< ...
- AOS Clustering on one Server
原文链接:http://www.cnblogs.com/JackyXu1981/articles/1287910.html AOS Clustering on one Server AOS Clust ...
- 《Android程序设计》课程学习
一.课件内容 2019-2010-1学期课件,点击查看 二.作业相关 上交作业的方法 访问ftp://192.168.42.254:22,登录后找到自己的姓名文件夹,放入作业即可.登录账号为stu2, ...