E - Farthest Nodes in a Tree
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的更多相关文章
- lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- Farthest Nodes in a Tree ---LightOj1094(树的直径)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no ...
- Farthest Nodes in a Tree (求树的直径)
题目链接,密码:hpu Description Given a tree (a connected graph with no cycles), you have to find the farthe ...
- LightOJ1094 - Farthest Nodes in a Tree(树的直径)
http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycle ...
- light oj 1094 Farthest Nodes in a Tree(树的直径模板)
1094 - Farthest Nodes in a Tree problem=1094" style="color:rgb(79,107,114)"> probl ...
- lightoj1094 - Farthest Nodes in a Tree
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limi ...
- LightOJ 1094 - Farthest Nodes in a Tree(树的直径)
http://acm.hust.edu.cn/vjudge/contest/121398#problem/H 不是特别理解,今天第一次碰到这种问题.给个链接看大神的解释吧 http://www.cnb ...
- LightOJ1257 Farthest Nodes in a Tree (II)(树的点分治)
题目给一棵树,边带有权值,求每一点到其他点路径上的最大权和. 树上任意两点的路径都可以看成是经过某棵子树根的路径,即路径权=两个点到根路径权的和,于是果断树分治. 对于每次分治的子树,计算其所有结点到 ...
- lght oj 1257 - Farthest Nodes in a Tree (II) (树dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1257 跟hdu2196一样,两次dfs //#pragma comment(l ...
随机推荐
- C++实现秒表
完整代码下载 思路概括:如果有键按下,判断按下的是什么键并处理.没有键按下,计时.传统的Sleep无法满足秒表精确到百毫秒的需求,这里使用更精确的clock,clock的作用是统计从程序开始运行到现在 ...
- vue - Babel之babel-polyfill、babel-runtime、transform-runtime
引言 babel默认只转换新的 JavaScript 语法,比如箭头函数.扩展运算(spread). 不转换新的 API,例如Iterator.Generator.Set.Maps.Proxy.Ref ...
- AI领域:如何做优秀研究并写高水平论文?
来源:深度强化学习实验室 每个人从本科到硕士,再到博士.博士后,甚至工作以后,都会遇到做研究.写论文这个差事.论文通常是对现有工作的一个总结和展示,特别对于博士和做研究的人来说,论文则显得更加重要. ...
- 深度强化学习(DRL)专栏开篇
2015年,DeepMind团队在Nature杂志上发表了一篇文章名为"Human-level control through deep reinforcement learning&quo ...
- centos7中安装redis
http://www.open-open.com/lib/view/open1426468117367.html https://www.cnblogs.com/cndavidwang/p/64294 ...
- 创建Windows10无人值守(自动应答文件)教程
一.准备工作 系统要求: Windows10 1809版本 工具下载: 镜像:Windows10,任何一个版本都可以,我使用的是1909版本 ed2k://|file|cn_windows_10_bu ...
- spring04
今天设计到的内容是是spring的sqel和ioc的生命周期和bean的后置处理器 具体的测试文件如下: <?xml version="1.0" encoding=" ...
- orm层面的删除的注意事项
orm层面的删除 当两张表的外键约束设置为RESTRICT or ACTION时,在sql的层面上想要删除父级表的数据时吗,mysql会拒绝删除,但是 使用orm的delete还是会删除父级表的数据. ...
- Javascript 入门 document
JS可以在hmtl中直接插入文本. <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...
- javascript入门 之 ztree(二 标准json数据)
1.代码 <!DOCTYPE html> <HTML> <HEAD> <TITLE> ZTREE DEMO - Standard Data </T ...