[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\)*边权+( ...
 
随机推荐
- 009-Spring Boot 事件监听、监听器配置与方式、spring、Spring boot内置事件
			
一.概念 1.事件监听的流程 步骤一.自定义事件,一般是继承ApplicationEvent抽象类 步骤二.定义事件监听器,一般是实现ApplicationListener接口 步骤三.启动时,需要将 ...
 - 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_01 File类_1_File类的概述
 - Week3 - 397. Integer Replacement
			
Week3 - 397. Integer Replacement 397.Integer Replacement - Medium Given a positive integer n and you ...
 - springboot An incompatible version [1.1.32] of the APR based Apache Tomcat Native library is installed, while Tomcat requires version [1.2.14]
			
1.错误 An incompatible version [1.1.32] of the APR based Apache Tomcat Native library is installed, wh ...
 - java中java.util.Date和java.sql.Date之间的关系和使用选择
			
关系: java.util.Date是java.sql.Date的父类 区别:(java.sql.Date包含年月日信息,java.util.Date包含年月日时分秒) 1:“规范化”的java.sq ...
 - 百度地图转腾讯地图腾讯地图转百度地图(还有方法二就是使用百度地图api 转火星坐标)
			
public static double pi = 3.141592653589793 * 3000.0 / 180.0; /** * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转 ...
 - 2019寒假作业二:PTA7-1币值转换
			
7-1 币值转换 (20 分) 输入一个整数(位数不超过9位)代表一个人民币值(单位为元),请转换成财务要求的大写中文格式.如23108元,转换后变成“贰万叁仟壹百零捌”元.为了简化输出,用小写英文字 ...
 - Mysql UPF 安装文档
			
一.mysql UDF 简介: github地址: http://www.mysqludf.org/lib_mysqludf_preg 二.mysql UDF 下载地址: https://github ...
 - python字符串学习总结
			
python字符串是不可变类型 所以没有添加和删除操作,更改元素,不会更改元素本身,可以用id(str) 测试,只有从新赋值新的对象才有效果.
 - .net core jessetalk资料合集
			
资料推荐集合贴 By Jesse • 2018-01-10 • 5064次浏览 流程图在线预览地址:https://9o90oe.axshare.com/#g=1&p=home OAuth2 ...