传送门  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. 2017 ACM/ICPC Asia Regional Shenyang Online:number number number hdu 6198【矩阵快速幂】

    Problem Description We define a sequence F: ⋅ F0=0,F1=1;⋅ Fn=Fn−1+Fn−2 (n≥2). Give you an integer k, ...

  2. ansible基础☞安装方法

    一 需要安装些什么 Ansible默认通过 SSH 协议管理机器. 安装Ansible之后,不需要启动或运行一个后台进程,或是添加一个数据库.只要在一台电脑(可以是一台笔记本)上安装好,就可以通过这台 ...

  3. EF ObjectStateManager无法跟踪具有相同键的多个对象 标签: EasyUIc# 2015-09-05 11:01 1181人阅读

    最近做一个项目,因为是重构,好多代码是搬过来的,但是因为框架不同,所以搬过来也出现了很多问题,前几天在调试的时候,就碰到一个EF框架经常出现的问题:ObjectStateManager中已存在具有同一 ...

  4. Python语言的特点

  5. Namenode文件系统命名空间映像文件及修改日志

  6. 「HNOI2015」菜肴制作

    「HNOI2015」菜肴制作 这道题想到了其实还挺水的,一开始我直接用小根堆拓扑然后就爆0了,然后我又用十万个堆搜索,T30,还是xkl告诉我要倒着拓扑. 首先要建反图,对于入度为0的点,较小的点先输 ...

  7. [ZJOI2007] 小Q的矩阵游戏 (模板—Dinic)

    B. 矩阵游戏 题目描述 小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏——矩阵游戏.矩阵游戏在一个N*N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每次可以对该矩阵进行 ...

  8. day5_python之hashlib模块

    用来校验文本内容hash:一种算法 ,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法三个特点:1.内容相同则h ...

  9. eclipse maven项目导出所使用的jar包

    在eclipse中定位到maven项目的pom.xml文件右击pom.xml文件,选择Run As-->Maven build…在打开的页面中,GOLAS栏输入“dependency:copy- ...

  10. [C#] WebClient性能优化

    WebClient缺省是为了安全和方便,不是为了性能.所以,当你打算做压力测试的时候,就会发现WebClient很慢. WebClient性能很差,主要原因有: 1.它缺省会使用IE的代理设置,而IE ...