传送门  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. Uva 10334

    UVa 10334 这道题几乎和UVa 495是一样的. #include<iostream> #include<cstdio> #define mod 1000000 usi ...

  2. iOS 9适配系列教程:后台定位

    http://www.cocoachina.com/ios/20150624/12200.html Demo:GitHub地址 [iOS9在定位的问题上,有一个坏消息一个好消息]坏消息:如果不适配iO ...

  3. Python基础:03序列:字符串、列表和元组

    一:序列 1:连接操作符(+) 这个操作符允许把一个序列和另一个相同类型的序列做连接,生成新的序列.语法如下:sequence1 + sequence2 该表达式的结果是一个包含sequence1和s ...

  4. mysql的root密码忘记解决方

    mysql的root密码忘记解决方 没关注第一步,直接从第二步开始,(可以参看上一篇,先停止mysql服务).然后从第二步开始. 我启动mysql用的命令是/etc/init.d/mysql  sta ...

  5. jmter正则表达式提取器

    1.若返回的body内容为空,仅有 header值,则: \s代表为空 2.使用Debug来调试

  6. 1、Ubuntu 16.04 安装.net core

    Register the Microsoft key register the product repository Install required dependencies 参考网址:https: ...

  7. poj 1066 Treasure Hunt (Geometry + BFS)

    1066 -- Treasure Hunt 题意是,在一个金字塔中有一个宝藏,金字塔里面有很多的墙,要穿过墙壁才能进入到宝藏所在的地方.可是因为某些原因,只能在两个墙壁的交点连线的中点穿过墙壁.问最少 ...

  8. HDU1358 Period 题解 KMP算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目大意:给你一个长度为 \(n\) 的字符串 \(s\) ,那么它有 \(n\) 个前缀. 对 ...

  9. SSM项目整合第一步 注册登陆实现

    SSM项目整合第一步  注册: 项目目录: 一.数据库建表: 源码: ; -- ---------------------------- -- Table structure for t_user - ...

  10. HDU 1236

    水题~~但我做了很久: 题意:是中国人都懂了 思路:结构体排序: 以后要多用用重定义的排序手段,!!!!!多用!!多用!!多用!! #include<iostream> #include& ...