传送门  https://ac.nowcoder.com/acm/contest/1109#question

刚开始吓得我以为要搞树分治,差点就捞了哦!

这个定理要铭记于心啊!!!

#include<cstring>
#include<iostream>
#include<queue>
#include<algorithm>
#include<cstdio>
#define maxn 100010
using namespace std;
typedef long long ll;
int n, m;
struct Node {
int p;
int len;
Node(int a, int b) :p(a), len(b) {}
};
vector<Node>G[maxn];
void insert(int be, int en, int len) {
G[be].push_back(Node(en, len));
}
ll dis[maxn];
ll dis1[maxn];
ll dis2[maxn];
int dfs(int x, ll dep, int fa, ll d[]) {
d[x] = dep;
for (int i = 0; i < G[x].size(); i++) {
int p = G[x][i].p;
if (p == fa) continue;
dfs(p, dep + G[x][i].len, x, d);
}
return 0;
}
int main() {
while (~scanf("%d", &n)) {
int be, en, len;
for (int i = 0; i < n - 1; i++){
scanf("%d %d %d", &be, &en, &len);
insert(be, en, len);
insert(en, be, len);
}
int s=1, t=1;
dfs(1, 0, -1, dis);
for (int i = 1; i <= n; i++) {
if (dis[i] > dis[s]) s = i;
}
dfs(s, 0, -1, dis1);//dis1从s开始
for (int i = 1; i <= n; i++) {
if (dis1[i] > dis1[t]) t = i;
}
//t从s开始
dfs(t, 0, -1, dis2);
ll ans = 0;
for (int i = 1; i <= n; i++) {
ans += max(dis1[i], dis2[i]);
}
ans -= dis1[t];
printf("%lld\n", ans);
for (int i = 0; i < maxn; i++) G[i].clear();
}
return 0;
}

  

牛客国庆 Day4 H 巧妙的用树的直径!!的更多相关文章

  1. 牛客国庆训练 H.千万别用树套树

    链接https://ac.nowcoder.com/acm/contest/1108/H 国庆队内训练的题,当时还完全没思路,就没补.现在会树状数组了,倒是能想一想,不过网上题解好多用线段树传数组的? ...

  2. 牛客国庆集训派对Day6 A Birthday 费用流

    牛客国庆集训派对Day6 A Birthday:https://www.nowcoder.com/acm/contest/206/A 题意: 恬恬的生日临近了.宇扬给她准备了一个蛋糕. 正如往常一样, ...

  3. 2019牛客国庆集训派对day5

    2019牛客国庆集训派对day5 I.Strange Prime 题意 \(P=1e10+19\),求\(\sum x[i] mod P = 0\)的方案数,其中\(0 \leq x[i] < ...

  4. 牛客练习赛11 假的字符串 (Trie树+拓扑找环)

    牛客练习赛11 假的字符串 (Trie树+拓扑找环) 链接:https://ac.nowcoder.com/acm/problem/15049 来源:牛客网 给定n个字符串,互不相等,你可以任意指定字 ...

  5. 2018 牛客国庆集训派对Day4 - H 树链博弈

    链接:https://ac.nowcoder.com/acm/contest/204/H来源:牛客网 题目描述 给定一棵 n 个点的树,其中 1 号结点是根,每个结点要么是黑色要么是白色 现在小 Bo ...

  6. 牛客国庆集训派对Day4 J-寻找复读机

    链接:https://www.nowcoder.com/acm/contest/204/J 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...

  7. 牛客国庆集训派对Day4 I-连通块计数(思维,组合数学)

    链接:https://www.nowcoder.com/acm/contest/204/I 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...

  8. 牛客国庆集训派对Day1 L-New Game!(最短路)

    链接:https://www.nowcoder.com/acm/contest/201/L 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...

  9. 牛客国庆集训派对Day1-C:Utawarerumono(数学)

    链接:https://www.nowcoder.com/acm/contest/201/C 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...

随机推荐

  1. JavaScript for循环 while循环

    循环可以将代码块执行指定的次数. JavaScript 循环 如果您希望一遍又一遍地运行相同的代码,并且每次的值都不同,那么使用循环是很方便的. 我们可以这样输出数组的值: 一般写法: documen ...

  2. PHP判断图片格式的七种方法小结

    <?php $imgurl = "http://www.jb51.net/images/logo.gif"; //方法1 echo $ext = strrchr($imgur ...

  3. JPA中id前面有空格导致的"Column 'id' not found"问题

    问题背景 昨晚有个同事发生了一个神奇的问题,一如既往的问题,一如既然的用我写的BEJSON-JAVA代码生成器生成,却发现一直提示Column 'id' not found.这就很TM神奇了 2018 ...

  4. java代码简单实现栈

    1. 基于数组简单实现 /** * @author <a herf="mailto:yanwu0527@163.com">XuBaofeng</a> * @ ...

  5. SDUT-3342_数据结构实验之二叉树三:统计叶子数

    数据结构实验之二叉树三:统计叶子数 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知二叉树的一个按先序遍历输入的字符 ...

  6. mysql 表名和字段、备注

    select t1.table_schema ,t1.table_name ,t2.ordinal_position ,t2.column_name ,t2.data_type ,t2.charact ...

  7. PyTorch入门学习(二):Autogard之自动求梯度

    autograd包是PyTorch中神经网络的核心部分,简单学习一下. autograd提供了所有张量操作的自动求微分功能. 它的灵活性体现在可以通过代码的运行来决定反向传播的过程, 这样就使得每一次 ...

  8. Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1) 题解

    A..B略 C 对当前的值排序,再二分答案,然后对于(i%x==0 && i%y==0)放入大的,再放其他的贪心解决即可. #include<iostream> #incl ...

  9. oracle用Where子句替换HAVING子句

    避免使用HAVING子句, HAVING 只会在检索出所有记录之后才对结果集进行过滤. 这个处理需要排序,总计等操作. 如果能通过WHERE子句限制记录的数目,那就能减少这方面的开销. 例如: 低效: ...

  10. Getting started with the basics of programming exercises_4

    1.编写一个删除C语言程序中所有的注释语句的程序.要正确处理带引号的字符串与字符串常量,C语言中程序注释不允许嵌套. #include<stdio.h> void rcomment(int ...