传送门  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. hibernate 出现Caused by: java.sql.SQLException: Column 'id' not found.异常

    用hibernate进行映射查询时,出现Caused by: java.sql.SQLException: Column 'id' not found 异常,检查数据库表及映射都有id且已经正确映射, ...

  2. java 根据秘钥,对数据进行加解密

    package test; import com.alibaba.fastjson.JSONObject; import sun.misc.BASE64Decoder; import sun.misc ...

  3. day6_python序列化之 json & pickle & shelve 模块

    一.json & pickle & shelve 模块 json,用于字符串 和 python数据类型间进行转换pickle,用于python特有的类型 和 python的数据类型间进 ...

  4. H3C 典型数据链路层标准

  5. 横向tab计算滚动位置

    React横向滚动计算 class Footer extends React.Component { handleClick(e) { const offset = 150; // 指定偏移量 thi ...

  6. poj 3601Tower of Hanoi

    Tower of Hanoi Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 1895   Accepted: 646 De ...

  7. hdu 1026 Ignatius and the Princess I(bfs)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  8. eclipse本地启动tomcat报错集锦

    1.eclipse本地添加tomcat服务器    打开Eclipse,单击“window”菜单,选择下方的“Preferences”: 找到Server下方的Runtime Environment, ...

  9. H3C 用三层交换机实现VLAN间路由

  10. Redis 命令行工具能这样用你知道了吗?

    我们天天都在使用 Redis 内置的命令行工具 redis-cli,久而久之以为它就是一个简单的交互式 Redis 数据结构手工操作程序,但是它背后强大的功能绝大多数同学可能闻所未闻.本节我们一起来挖 ...