[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\)*边权+( ...
随机推荐
- request.getParameter() 和request.getAttribute()
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/wrs120/article/details/79287607[相同点] 1.都是HttpServle ...
- 内网渗透 - 提权 - Windows
MS提权 MS16- MS16- 提权框架 Sherlock 信息收集 ifconfig -a cat /etc/hosts arp -a route -n cat /proc/net/* ping扫 ...
- Vue路由注意事项
一.vue中路由的使用 1.定义组件 <template> <div class="hello"> <h1 @click="info&quo ...
- js获取指定字符后面的字符
function getCaption(obj){ var index=obj.lastIndexOf("\-"); obj=obj.substring(index+1,obj.l ...
- SQL基本语法和书写格式
插入 insert [into] 表名 [(列名列表)] values (值列表) insert into 新表名 (列名列表) select 列名列表 from 表名 select 表名.列名 in ...
- Ubuntu 12.04安装Gitlab及问题解决
最近看了下Git,并且之前听同学说过gitlab这个东西,就想自己也搭建一个gitlab,做一个像github那样的代码管理站点,现在的gitlab要安装确实是非常非常方便, https://abou ...
- (一:NIO系列)JAVA NIO 简介
出处:JAVA NIO 简介 Java 中 New I/O类库 是由 Java 1.4 引进的异步 IO.由于之前老的I/O类库是阻塞I/O,New I/O类库的目标就是要让Java支持非阻塞I/O, ...
- appscan 历史版本下载
https://www.cnblogs.com/hua198/p/11120916.html
- QT中使用Event Filter监听button事件,Release后button不见
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhenyu5211314/article/details/27201043 问题RT,在程序中我使用 ...
- Jquery实例链接
jquery学习笔记 jquery实现全选,反选,取消的操作 左侧菜单收缩的实现(包括,筛选器,addclass.removeclass.绑定事件,链式编程) 模态对话框实现增加删除表格里面的内容 j ...