1、题意:给个树,边的权值=两边的点数差*此边的长度,求所有边的权值和

2、分析:真不想说啥了。。。dfs即可

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
#define M 2000010

inline int read(){
    char ch = getchar(); int x = 0, f = 1;
    while(ch < '0' || ch > '9'){
        if(ch == '-') f = -1;
        ch = getchar();
    }
    while('0' <= ch && ch <= '9'){
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}

struct Edge{
    int u, v, w, next;
} G[M];
int head[M], tot;
int size[M];
LL ans;
int n;

inline void add(int u, int v, int w){
    G[++ tot] = (Edge){u, v, w, head[u]};
    head[u] = tot;
}

inline void dfs(int x, int fa){
    size[x] = 1;
    for(int i = head[x]; i != -1; i = G[i].next) if(G[i].v != fa){
        dfs(G[i].v, x);
        size[x] += size[G[i].v];
        ans += (LL)abs(n - size[G[i].v] - size[G[i].v]) * (LL)G[i].w;
    }
}

int main(){
    n = read();
    memset(head, -1, sizeof(head));
    for(int i = 1; i < n; i ++){
        int u = read(), v = read(), w = read();
        add(u, v, w); add(v, u, w);
    }
    dfs(1, 0);
    printf("%lld\n", ans);
    return 0;
} 

BZOJ2435——[Noi2011]道路修建的更多相关文章

  1. BZOJ2435:[NOI2011]道路修建 (差分)

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

  2. BZOJ2435 [Noi2011]道路修建 【树形Dp 吧。。】

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

  3. BZOJ2435 NOI2011道路修建

    要多简单有多简单.然而不知道为啥在luogu上过不掉. #include<iostream> #include<cstdio> #include<cmath> #i ...

  4. 【题解】 bzoj2435: [Noi2011]道路修建 (傻逼题)

    bzoj2435,懒得复制,戳我戳我 Solution: 模拟即可(有点傻逼啊 Code: //It is coded by Ning_Mew on 5.13 #include<bits/std ...

  5. BZOJ2435: [Noi2011]道路修建

    这种水题真是……没一次AC都不好意思见人啊 P.S. LINUX无限栈真是爽炸了… 我爱递归 /**************************************************** ...

  6. 【DFS】bzoj2435 [Noi2011]道路修建

    两遍DFS.第一遍统计以每个点为根的子树大小,第二遍更新答案. #include<cstdio> #include<iostream> using namespace std; ...

  7. 【BZOJ-2435】道路修建 (树形DP?)DFS

    2435: [Noi2011]道路修建 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3115  Solved: 1002[Submit][Statu ...

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

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

  9. NOI2011道路修建

    2435: [Noi2011]道路修建 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1974  Solved: 550[Submit][Status ...

随机推荐

  1. java1.8常用的函数式接口

    //常用函数式接口 final ; //num++; //第一个为传入参数的类型:第二个为返回数据的类型 Function<int[],String> function = (from) ...

  2. struts之类型转换

    Struts2的内置类型转换器: Struts2的内置类型转换器,可以为你处理大多数的类型转换,这包括了以下类型和String类型之间的转换! 1.String 将int,double,boolean ...

  3. C语言基础(一)

    7744问题(输出所有形如aabb的4位完全平方数) 方法1: #include<stdio.h> #include<math.h> int main (){ ;a<=; ...

  4. linux 安全狗

    下载 ;安装; service safedog start  或者 sdstart;sdui

  5. WebForm路由踩坑 ajax请求多次

    WebForm路由踩坑 再次接触Asp.Net WebForm已是4年后的今天,源起新入职的公司,一个老的项目. Web接触的少,那就多动手写写. WebForm1.aspx <body> ...

  6. 【跟着子迟品 underscore】如何优雅地写一个『在数组中寻找指定元素』的方法

    Why underscore (觉得这部分眼熟的可以直接跳到下一段了...) 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. ...

  7. Jumony Core 3,真正的HTML引擎,正式版发布

    Jumony是一个开源项目,已经有三年的历史了,在这三年中,秉承提供给.NET程序员完整的HTML掌控能力,Jumony历经无数次的改进,终于进入了一个新的阶段.Jumony Core 3是一个真正意 ...

  8. MyISAM 和InnoDB 的区别.(存储,索引, 事务, 锁)

    MyISAM类型的表强调的是性能,但是不支持事务.及外部键等高级功能. MySQL默认采用的是MyISAM. MyISAM不支持事务,而InnoDB支持.InnoDB的AUTOCOMMIT默认是打开的 ...

  9. js jquery 页面加载初始化方法

    js jquery 页面加载初始化方法 一.js页面加载初始化方法 // 1.在body里面写初始化方法. <body onload='init()'> </body> < ...

  10. AutoVFL(适配)

    1.添加约束(系统) a.一个约束(上下左右) +(instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)att ...