传送门  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. wepy中组件之间通信方法

    events events是WePY组件事件处理函数对象,存放响应组件之间通过broadcast.emit.$invoke所传递的事件的函数. $broadcast —— 父往子传 $broadcas ...

  2. 在WPF中使用谷歌地图和高德地图

    原文:在WPF中使用谷歌地图和高德地图 在桌面软件开发中可能会遇到这样的需求:显示地图. 常用的地图API有Google Map和高德地图.二者都提供了各种平台的API. 为了方便集成,本文使用Jav ...

  3. WPF疑难杂症之二(全屏幕窗口)

    原文:WPF疑难杂症之二(全屏幕窗口) 近日的学习中遇到一个非常奇怪的问题:用XAML文件创建了一个全屏幕窗口,然后,在窗口中建立了一个非常简单的动画.一切都在我的掌控之中,实现非常的顺利. WPF中 ...

  4. 2018-9-1-win10-uwp-轻量级-MVVM-框架入门-2.1.5.3199

    title author date CreateTime categories win10 uwp 轻量级 MVVM 框架入门 2.1.5.3199 lindexi 2018-09-01 16:24: ...

  5. 禁用GPU版本TensorFlow,切换到CPU版本TensorFlow。

    #禁用gpu版本TensorFlow,因为CUDA号码从0开始,这里直接让CUDA使用-1的GPU,自然就无法使用gpu了. 代码前面加入: import osos.environ["CUD ...

  6. js键盘按下移动元素

    文章地址 https://www.cnblogs.com/sandraryan/ 功能: 点击上下左右按钮,移动元素 <!DOCTYPE html> <html lang=" ...

  7. halcon坐标转换(机器人坐标转换用)

    #图像坐标r:=[431, 355, 507, 53, 507]c:=[505, 543, 316, 127, 883]#物理坐标(例如机器人坐标)r1:=[0, 2.0, -2.0, 10, -2. ...

  8. UA判断打开页面的环境,然后在callBack写相应环境下的回调函数

    这是js代码 /* * 2016.11.10 * SunJingxin * V 1.0.0 * */ (function(){ /* * 使用方法: * 一.引入ua.js * 二.直接调用 Mobi ...

  9. [转]分布式session的几种实现方式

    我们应当对产生的Session进行处理,通过粘性Session,Session复制或Session共享等方式保证用户的体验度. 以下我将说明5种Session处理策略,并分析其优劣性. 第一种:粘性s ...

  10. javaScript 删除事件 弹出确认 取消对话框

    javaScript 删除事件 弹出确认 取消对话框 1. <a href="javascript:if(confirm('确实要删除?'))location='http://www. ...