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. 偏函数(partial)

    from functools import partial def add(a,b,c,d): return a+b+c+d add = partial(add,1,2) print(add(3,4) ...

  2. HttpClient之HttpContext使用

    Multiple request sequences that represent a logically related session should be executed with the sa ...

  3. 2019.6.1 模拟赛——[ 费用流 ][ 数位DP ][ 计算几何 ]

    第一题:http://codeforces.com/contest/1061/problem/E 把点集分成不相交的,然后跑费用流即可.然而错了一个点. #include<cstdio> ...

  4. IIS的站点配置存储在applicationHost.config

    C:\Windows\System32\inetsrv\Config\applicationHost.config

  5. Hive if和coalesce函数

    链接:https://blog.csdn.net/qq_26442553/article/details/79465417 if 函数举例:

  6. 利用单臂路由实现VLAN间路由(有1个疑问)

    配置PC机: PC1:IP 192.168.1.1 :掩码:255.255.255.0:网关:192.168.1.254  VLAN 10 PC2:IP 192.168.2.1 :掩码:255.255 ...

  7. 【Python—待解惑的问题】

    疑问1: map()函数第一个参数接收函数,所以 dict.keys() 是函数?这个不是方法吗? map(dict.keys,s) s1.keys() == dict.keys(s1) 输入: Tr ...

  8. Codeforces 488D Strip (set+DP)

    D. Strip time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  9. python 大小写转换函数

    capitalize() 首字母大写,其余全部小写 upper() 全转换成大写 lower() 全转换成小写 title()  标题首字大写,如"i love python".t ...

  10. MVC5使用SignalR进行双向通信 (1)

    @a604572782 2015-08-10 09:01 字数 2133 阅读 1245 MVC5使用SignalR进行双向通信 (1) 配置SignalR 在NuGet中通过 install-pac ...