Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. The edges of the tree are weighted and undirected. That means you have to find two nodes in the tree whose distance is maximum amongst all nodes.

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case starts with an integer n (2 ≤ n ≤ 30000) denoting the total number of nodes in the tree. The nodes are numbered from 0 to n-1. Each of the next n-1 lines will contain three integers u v w (0 ≤ u, v < n, u ≠ v, 1 ≤ w ≤ 10000)denoting that node u and v are connected by an edge whose weight is w. You can assume that the input will form a valid tree.

Output

For each case, print the case number and the maximum distance.

Sample Input

2

4

0 1 20

1 2 30

2 3 50

5

0 2 20

2 1 10

0 3 29

0 4 50

Sample Output

Case 1: 100

Case 2: 80

题目大意:输入A,B,点以及a点b点之间的距离输出,,两点最远的距离

AC代码:

#include<cstdio>
#include<vector>
#include<iostream>
#include<cstring>
using namespace std;
struct stu{
int y,s;
};
vector<stu>ve[];
int ans=;
int xx;
int mark[];
void dfs(int x,int step){
if(step>ans){
ans=step;
xx=x;
}
for(int i=;i<ve[x].size();i++){
if(mark[ve[x][i].y]==){
mark[ve[x][i].y]=;
dfs(ve[x][i].y,step+ve[x][i].s);
mark[ve[x][i].y]=;
}
}
}
int main()
{
int t;
scanf("%d",&t);
for(int j=;j<=t;j++){
int a,b,c,n;
ans=;
scanf("%d",&n); for(int i=;i<n-;i++){
scanf("%d %d %d",&a,&b,&c);
ve[a].push_back({b,c});
ve[b].push_back({a,c});
} memset(mark,,sizeof(mark));
mark[]=;
dfs(,);
memset(mark,,sizeof(mark));
mark[xx]=;
dfs(xx,);
printf("Case %d: %d\n",j,ans);
for(int i=;i<n;i++){
ve[i].clear();
}
}
return ;
}

E - Farthest Nodes in a Tree的更多相关文章

  1. lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】

    1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

  2. Farthest Nodes in a Tree ---LightOj1094(树的直径)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no ...

  3. Farthest Nodes in a Tree (求树的直径)

    题目链接,密码:hpu Description Given a tree (a connected graph with no cycles), you have to find the farthe ...

  4. LightOJ1094 - Farthest Nodes in a Tree(树的直径)

    http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycle ...

  5. light oj 1094 Farthest Nodes in a Tree(树的直径模板)

    1094 - Farthest Nodes in a Tree problem=1094" style="color:rgb(79,107,114)"> probl ...

  6. lightoj1094 - Farthest Nodes in a Tree

    1094 - Farthest Nodes in a Tree   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limi ...

  7. LightOJ 1094 - Farthest Nodes in a Tree(树的直径)

    http://acm.hust.edu.cn/vjudge/contest/121398#problem/H 不是特别理解,今天第一次碰到这种问题.给个链接看大神的解释吧 http://www.cnb ...

  8. LightOJ1257 Farthest Nodes in a Tree (II)(树的点分治)

    题目给一棵树,边带有权值,求每一点到其他点路径上的最大权和. 树上任意两点的路径都可以看成是经过某棵子树根的路径,即路径权=两个点到根路径权的和,于是果断树分治. 对于每次分治的子树,计算其所有结点到 ...

  9. lght oj 1257 - Farthest Nodes in a Tree (II) (树dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1257 跟hdu2196一样,两次dfs //#pragma comment(l ...

随机推荐

  1. ThinkPHP的几种路由形式总结

    本篇文章总结了ThinkPHP的几种路由形式,分别是普通模式.PATHINFO.REWRITE和兼容模式,希望对各位学习thinkphp的朋友有帮助! thinkphp官方文档里有详细的说明: Thi ...

  2. Excel知识点与技巧1

    1.工作区:方便两个工作表之间进行对比 2.工作表标签颜色 3.交换两列的次序 4.快速到达边界:即快速到达第一行或最后一行 5.冻结窗格:可以固定某几行或某几列一直存在于窗口,不会随着往下拉或往右拉 ...

  3. [Java8教程]Java8新特性进阶集合

    Java8新特性进阶集合 基于 AOP 抽离方法的重复代码 Java8:当 Lambda 遇上受检异常 Java8:对字符串连接的改进 Java8:Java8 中 Map 接口的新方法 Java8:当 ...

  4. 常见Web安全漏洞--------CSRF

    1,CSRF (Cross Site Request Forgery, 跨站域请求伪造),也可以说是模拟请求. 2,黑客获取到了token 令牌,发送恶意模拟请求,攻击网站,防御方法可以参考api 接 ...

  5. js 的 new 干了什么

  6. 数据挖掘入门系列教程(八)之使用神经网络(基于pybrain)识别数字手写集MNIST

    目录 数据挖掘入门系列教程(八)之使用神经网络(基于pybrain)识别数字手写集MNIST 下载数据集 加载数据集 构建神经网络 反向传播(BP)算法 进行预测 F1验证 总结 参考 数据挖掘入门系 ...

  7. iOS 第三方库

    网络 AFNetworking HTTP网络库 Reachability 网络监测 UI.布局 Masonry AutoLayout SnapKit AutoLayout Swift TOWebVie ...

  8. 模块 face_recognition 人脸识别

    face_recognition 人脸识别 api 说明 1 load_image_file 将img文件加载到numpy 数组中 2 face_locations 查找图像中所有面部和所有面部特征的 ...

  9. 小案例带你揭秘JS事件

    小案例带你揭秘JS事件 ### 什么是事件? 在js中一个事件的组成由那些呢? 谁触发事件:事件源 触发什么事件: 事件的类型 触发事件干什么事:事件处理函数 事件传播的过程 捕获阶段 就是从wind ...

  10. 玩转控件:对Dev的GridControl控件扩展

    缘由 一切实现来源于需求,目的在于不盲目造轮子,有小伙伴儿在看了<玩转控件:对Dev中GridControl控件的封装和扩展>文章后,私信作者说,因公司业务逻辑比较复杂,展示字段比较多,尤 ...