Description

Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their common friends. Since Jenny is Irish, Heidi thinks that this might be a prank. More precisely, she suspects that the message she is asked to deliver states: "Send the fool further!", and upon reading it the recipient will ask Heidi to deliver the same message to yet another friend (that the recipient has in common with Heidi), and so on.

Heidi believes that her friends want to avoid awkward situations, so she will not be made to visit the same person (including Jenny) twice. She also knows how much it costs to travel between any two of her friends who know each other. She wants to know: what is the maximal amount of money she will waste on travel if it really is a prank?

Heidi's n friends are labeled 0 through n - 1, and their network of connections forms a tree. In other words, every two of her friends ab know each other, possibly indirectly (there is a sequence of friends starting from a and ending on b and such that each two consecutive friends in the sequence know each other directly), and there are exactly n - 1 pairs of friends who know each other directly.

Jenny is given the number 0.

Input

The first line of the input contains the number of friends n (3 ≤ n ≤ 100). The next n - 1 lines each contain three space-separated integers uvand c (0 ≤ u, v ≤ n - 1, 1 ≤ c ≤ 104), meaning that u and v are friends (know each other directly) and the cost for travelling between u and vis c.

It is guaranteed that the social network of the input forms a tree.

Output

Output a single integer – the maximum sum of costs.

Examples
input
4
0 1 4
0 2 2
2 3 3
output
5
input
6
1 2 3
0 2 100
1 4 2
0 3 7
3 5 10
output
105
input
11
1 0 1664
2 0 881
3 2 4670
4 2 1555
5 1 1870
6 2 1265
7 2 288
8 7 2266
9 2 1536
10 6 3378
output
5551
Note

In the second example, the worst-case scenario goes like this: Jenny sends Heidi to the friend labeled by number 2 (incurring a cost of 100), then friend 2 sends her to friend 1 (costing Heidi 3), and finally friend 1 relays her to friend 4 (incurring an additional cost of 2).

题意:通俗一点,求树的每条分支的和,把最大的拿出来

解法:dfs,遍历到叶子就比较一下喽

 #include <bits/stdc++.h>
using namespace std;
int n;
vector<pair<int,int>>q[];
int sum;
int vis[];
void dfs(int x,int cot,int pre)
{
if(vis[x])
{
return;
}
vis[x]=;
for(int i=; i<q[x].size(); i++)
{
if(vis[q[x][i].first])
{
{
sum=max(sum,cot);
continue;
}
}
dfs(q[x][i].first,cot+q[x][i].second,x);
}
}
int main()
{
cin>>n;
for(int i=; i<n; i++)
{
int v,u,c;
cin>>v>>u>>c;
q[v].push_back({u,c});
q[u].push_back({v,c});
}
dfs(,,);
cout<<sum<<endl;
return ;
}

Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) J的更多相关文章

  1. Helvetic Coding Contest 2017 online mirror (teams allowed, unrated)

    G. Fake News (easy) time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) M

    Description The marmots have prepared a very easy problem for this year's HC2 – this one. It involve ...

  3. Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) A

    Description Your search for Heidi is over – you finally found her at a library, dressed up as a huma ...

  4. Helvetic Coding Contest 2019 online mirror (teams allowed, unrated)

    http://codeforces.com/contest/1184 A1 找一对整数,使x^x+2xy+x+1=r 变换成一个分式,保证整除 #include<iostream> #in ...

  5. Helvetic Coding Contest 2018 online mirror (teams allowed, unrated)F3 - Lightsabers (hard)

    题意:n个数字1-m,问取k个组成的set方案数 题解:假设某个数出现k次,那么生成函数为\(1+x+...+x^k\),那么假设第i个数出现ai次,结果就是\(\sum_{i=1}^m(1+x+.. ...

  6. CF 690C3. Brain Network (hard) from Helvetic Coding Contest 2016 online mirror (teams, unrated)

    题目描述 Brain Network (hard) 这个问题就是给出一个不断加边的树,保证每一次加边之后都只有一个连通块(每一次连的点都是之前出现过的),问每一次加边之后树的直径. 算法 每一次增加一 ...

  7. [Helvetic Coding Contest 2017 online mirror]

    来自FallDream的博客,未经允许,请勿转载,谢谢, 第一次在cf上打acm...和同校大佬组队打 总共15题,比较鬼畜,最后勉强过了10题. AB一样的题目,不同数据范围,一起讲吧 你有一个背包 ...

  8. 【Codeforces】Helvetic Coding Contest 2017 online mirror比赛记

    第一次打ACM赛制的团队赛,感觉还行: 好吧主要是切水题: 开场先挑着做五道EASY,他们分给我D题,woc什么玩意,还泊松分布,我连题都读不懂好吗! 果断弃掉了,换了M和J,然后切掉了,看N题: l ...

  9. Helvetic Coding Contest 2016 online mirror A1

    Description Tonight is brain dinner night and all zombies will gather together to scarf down some de ...

随机推荐

  1. Robotium结果的收集和失败重跑

    引用自 http://www.robotium.cn/archives/author/zered 测试用例: testsuite管理测试用例 测试结果的输出与收集? InstrumentationTe ...

  2. effctive C++ 读书笔记 条款 16

    条款16 成对使用new和delete时要採取同样形式 #include <iostream> #include <string> using namespace std; / ...

  3. Linq To Sql进阶系列(六)用object的动态查询与保存log篇

    动态的生成sql语句,根据不同的条件构造不同的where字句,是拼接sql 字符串的好处.而Linq的推出,是为了弥补编程中的 Data != Object 的问题.我们又该如何实现用object的动 ...

  4. SenTestingKit.framework的报错!

    本文转载至http://www.cocoachina.com/ask/questions/show/106912 ld: building for iOS Simulator, but linking ...

  5. VUE 之 生命周期

    1. Vue实例的生命周期分为8个周期 1.1 beforeCreate:在实例创建前 <div id="app"> {{ name }} <button @cl ...

  6. dubbo配置指南

    dubbo配置指南 SLA配置在此完成!Service Layer Agreement ApplicationConfig 应用配置,用于配置当前应用信息,不管该应用是提供者还是消费者. Regist ...

  7. Maven group, artifact or version defined in the pom file do not match the file ...

    在把library上传到bintray空间的时候报以下错误 Could not upload to 'https://api.bintray.com/content/ping/maven/comm-a ...

  8. Oracle数据库案例整理-Oracle系统执行时故障-断电导致数据文件状态变为RECOVER

    1.1      现象描写叙述异常断电.数据库数据文件的状态由ONLINE变为RECOVER. 系统显演示样例如以下信息:SQL>selectfile_name,tablespace_name, ...

  9. RabbitMQ使用简述

    RabbitMQ基于AMQP协议. AMQP:是一个高级抽象层消息通信协议,RabbitMQ是AMQP协议的实现 RabbitMQ使用:Exchange(交换机)根据routing-key(路由选择键 ...

  10. java在某个日期上添加n天的方法实现

    //得到添加n天后的时间字符串 public String getAddDate(Date date,int n){ //格式转换 SimpleDateFormat sdf = new SimpleD ...