bzoj 2435: [Noi2011]道路修建【树形dp】
dp求size和deep,然后对每条边模拟求代价即可
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=1000005;
int n,h[N],cnt,si[N],de[N];
long long ans;
struct qwe
{
int ne,no,to,va;
}e[N<<1];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void add(int u,int v,int w)
{
cnt++;
e[cnt].ne=h[u];
e[cnt].no=u;
e[cnt].to=v;
e[cnt].va=w;
h[u]=cnt;
}
void dfs(int u,int fa)
{
si[u]=1,de[u]=de[fa]+1;
for(int i=h[u];i;i=e[i].ne)
if(e[i].to!=fa)
{
dfs(e[i].to,u);
si[u]+=si[e[i].to];
}
}
int main()
{
n=read();
for(int i=1;i<n;i++)
{
int x=read(),y=read(),z=read();
add(x,y,z),add(y,x,z);
}
dfs(1,0);
for(int i=1;i<=cnt;i+=2)
{
int x=de[e[i].no]>de[e[i].to]?e[i].no:e[i].to;
ans+=1ll*abs(n-2*si[x])*e[i].va;
}
printf("%lld\n",ans);
return 0;
}
bzoj 2435: [Noi2011]道路修建【树形dp】的更多相关文章
- bzoj 2435: [Noi2011]道路修建 树上 dp
2435: [Noi2011]道路修建 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...
- BZOJ 2435: [Noi2011]道路修建( dfs )
NOI的水题...直接一遍DFS即可 ------------------------------------------------------------------------- #includ ...
- BZOJ 2435: [Noi2011]道路修建 dfs搜图
2435: [Noi2011]道路修建 Description 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他 ...
- 【bzoj2435】[NOI2011]道路修建 树形dp
题目描述 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿意修建恰好 n – 1条双向道路. 每条道路的修 ...
- [luogu2052 NOI2011] 道路修建 (树形dp)
传送门 Description 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿 意修建恰好 n – 1 ...
- [noi2011]道路修建 树形dp
这道题可以说是树形dp的入门题,也可以看成是一道检验[树]这个数据结构的题目: 这道题只能bfs,毕竟10^6的复杂度win下肯定爆栈了: 但是最恶心的还不是这个,实测用printf输出 用cout输 ...
- 洛谷P2052 [NOI2011]道路修建(树形DP)
题目描述 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿 意修建恰好 n – 1 条双向道路. 每条道 ...
- bzoj 2435: [Noi2011]道路修建
Description 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿 意修建恰好 n – 1条双向道 ...
- 2435: [Noi2011]道路修建
2435: [Noi2011]道路修建 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2188 Solved: 639[Submit][Status ...
随机推荐
- 利用卷积神经网络(CNN)构造社区问答系统
/* 版权声明:能够随意转载,转载时请标明文章原始出处和作者信息 .*/ author: 张俊林 ...
- Codeforces Round #148 (Div. 1)
A wool sequence 表示一个序列中能够找到一个连续的子区间使得区间异或值为0 那么求的是不含这样的情况的序列个数 题目中数据范围是.在0~2^m - 1中选n个数作为一个序列 n和m都是1 ...
- MeiTuanLocateCity
https://github.com/eltld/MeiTuanLocateCity
- openwrt mt7620 内存大小检测
单独编译内核: make V=s target/linux/install 相调函数调用流程: init/main.c : start_kernel() -> setup_arch(&c ...
- 最长公共字序列.cpp
<span style="color:#993399;">/* By yuan 2014/6/21 At nwpu.xf 1041.最长公共子序列 时限:1000ms ...
- MVC 基于FormsAuthentication 方式的权限验证
1.登录的代码 1 [HttpPost] 2 public ActionResult Index(User entity) 3 { 4 User user = GetUser(entity.Name, ...
- python day-01 (python基础知识1)
1.计算机组成(参与运算的) 主板+CPU+内存 2.python 简史 解释型语言,弱类型语言(eg:a 可以表示很多意思 a=1 或 a = “小红”) 3.数据类型: int(整数类型): ...
- Mac开发必备工具(二)—— iTerm 2
iTerm 2 简介 iTerm 2 is a terminal emulator for Mac OS X that does amazing things. iTerm 2 有很多能够提升效率的实 ...
- BigDecimal快速使用
double类型最多支持16位有效数字,且最大值只支持10^308次方,大一点的数字会变为科学计数法,小数精度不够等有一系列不方便的问题: 引进BigDecimal解决此类麻烦,弊端,BigDecim ...
- Why you shouldn’t connect your mobile application to a database
BY CRAIG CHAPMAN · PUBLISHED 2015-07-02 · UPDATED 2015-07-02 Working at Embarcadero, I frequently ...