http://www.lydsy.com/JudgeOnline/problem.php?id=2435

noi 你为什么那么diao, 这种世纪水题刷一道少一道啊。。。 我原来还以为是两边的联通块大小按已经连接上的点来算,然后发现是按照最后的联通块来算的(' '    ) 直接每个点 abs(n - 2 * size[x]) * dis(边权)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std; const int maxn = 1000100; struct edge {
int t, d;
edge* next;
}e[maxn * 2], *head[maxn]; int ne = 0;
int n, m; void addedge(int f, int t, int d) {
e[ne].t = t, e[ne].d = d, e[ne].next = head[f], head[f] = e + ne ++;
} int sta[maxn], top = 0, size[maxn], dis[maxn]; void dfs(int x, int fa) {
sta[++ top] = x; size[x] = 1;
for(edge* p = head[x]; p; p = p-> next) {
if(p-> t != fa) dis[p-> t] = p-> d, dfs(p-> t, x), size[x] += size[p-> t];
}
} int int_get() {
int x = 0; char c = (char)getchar(); bool f =0 ;
while(!isdigit(c)) {
if(c == '-') f = 1;
c = (char)getchar();
}
while(isdigit(c)) {
x = x * 10 + (int)(c - '0');
c = (char)getchar();
}
if(f) x = -x;
return x;
} void read() {
n = int_get();
for(int i = 1; i < n; ++ i) {
int u, v, w;
u = int_get(), v = int_get(), w = int_get();
addedge(u, v, w), addedge(v, u, w);
}
} long long ans = 0; void sov() {
dfs(1, 0);
for(int i = top; i >= 2; -- i) {
ans += (long long)(abs(n - 2 * size[sta[i]])) * (long long)dis[sta[i]];
}
printf("%lld\n", ans);
} int main() {
//freopen("test.in", "r", stdin);
read(), sov();
return 0;
}

bzoj 2435的更多相关文章

  1. bzoj 2435: [Noi2011]道路修建 树上 dp

    2435: [Noi2011]道路修建 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...

  2. BZOJ 2435: [Noi2011]道路修建( dfs )

    NOI的水题...直接一遍DFS即可 ------------------------------------------------------------------------- #includ ...

  3. BZOJ 2435: [Noi2011]道路修建 dfs搜图

    2435: [Noi2011]道路修建 Description 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他 ...

  4. BZOJ 2435:[Noi2011]道路修建(树型DP)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2435 题意:中文题意. 思路:很简单的树形DP,sz记录儿子有多少个和cur记录走的哪条弧,然后直接 ...

  5. BZOJ 2435 道路修建 NOI2011 树形DP

    一看到这道题觉得很水,打了递归树形DP后RE了一组,后来发现必须非递归(BFS) 递归版本84分: #include<cstdio> #include<cstring> #in ...

  6. BZOJ 2435 NOI2011 道路建设 BFS/DFS

    标题效果:给定一个树(直接将树.不要贪图生成树图!).寻找每条边权值*分差的两侧之间 BFS水必须是能 竟DFS能够住...系统堆栈可能有些不够,我们可以使用内联汇编手册中大型系统堆栈 详见代码 这个 ...

  7. bzoj 2435: [Noi2011]道路修建

    Description 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿 意修建恰好 n – 1条双向道 ...

  8. bzoj 2435 道路修建

    Written with StackEdit. Description 在 \(W\) 星球上有 \(n\) 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道路使得国家之间连通.但是 ...

  9. bzoj 2435 BFS

    我们可以先将无根树变成有根树,随便选一个点当根就行了,我们选1,bfs求出来每个点 的size值,代表以它为根节点的子树中有多少个节点,(dfs可能会爆栈),然后再对于每一条 边算就好了 我tle了, ...

随机推荐

  1. Adobe Flash Player30.0.0.113离线安装包

    远程控制工具  www.iis7.net/a/lm/yczmljgj/ Adobe2017年7月26日宣布,计划终结 Flash 浏览器插件,并在2020年停止开发和分发这款插件.Adobe 建议内容 ...

  2. DM9000网卡驱动深度分析

    一.dm9000_porbe函数分析 不同于u-boot代码,tq2440中的DM9000更加复杂,需要分析的点也很多: /* * Search DM9000 board, allocate spac ...

  3. python3下tomorow模块报语法错误def async(n, base_type, timeout=None): ^ SyntaxError: invalid syntax

    python3 pip 安装tomorrow模块,调用时候会报错:def async(n, base_type, timeout=None): ^ SyntaxError: invalid synta ...

  4. vue将页面导出成pdf

    npm i jspdf-html2canvas prinOut(){ // 导出pdf let page = document.querySelector('.app-main'); // page ...

  5. js中Array方法重写(二):myForEach;myEvery;mySome;myFilter;myReduce

    一.myForEach //myForeach 数组每个元素都执行一次回调函数 Array.prototype.myForEach = function(callback){ for(var i = ...

  6. html&css面试笔记

    1.CSS选择器有哪些?它们的优先级是怎样的? 选择器类型: id选择器 ( # myid) 类选择器 (.myclassname) 标签选择器 (div, h1, p) 相邻选择器 (h1 + p) ...

  7. Linux基本常用命令|ubuntu获取root权限

    我用的是ubuntu12.4系统,因为默认是没有获取root的权限的 下边讲解怎么获取root权限 在终端中输入: sudo passwd root Enter new UNIX password: ...

  8. Java JsonPath grab InvalidPathException in code, you must be catching Java 7's java.nio.file.InvalidPathException instead of JsonPath's com.jayway.jsonpath.InvalidPathExceptio

    I am using JsonPath and am able to parse my data and get the values when the path provided is correc ...

  9. error C2664: “ATL::CStringT<BaseType,StringTraits>::Remove”: 不能将参数 1 从“const char [2]”转换为“char”

    转自VC错误:http://www.vcerror.com/?p=1395 问题描述: 代码: CString str("asdfafda"); str.Remove(" ...

  10. 【Jmeter基础】 Linux上运行Jmeter

    Jmeter 是一个强大的并发压测工具,常用于性能测试,压力测试,接口测试等领域.本文将介绍如下内容: 1. Jmeter 执行阶梯式压测 2. Jmeter 如何压测网站的js,css 等资源 3. ...