Farthest Nodes in a Tree ---LightOj1094(树的直径)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1094
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 |
Output for 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 |
Case 1: 100 Case 2: 80 |
Notes
Dataset is huge, use faster i/o methods.
刚读的时候就是感觉是最长璐, 后来知道就是树的直径;
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std; #define N 32200
#define INF 0xfffffff int Head[N], cnt, Max, Index, vis[N], d[N];
struct node
{
int v, L, next;
}e[N*]; void Add(int u, int v, int L)
{
e[cnt].L = L;
e[cnt].v = v;
e[cnt].next = Head[u];
Head[u] = cnt++;
}
void bfs(int u)
{
memset(vis, , sizeof(vis));
queue<int>Q;
int p, q;
d[u]=;
vis[u]=;
Q.push(u);
while(Q.size())
{
p=Q.front();
Q.pop();
for(int i=Head[p]; i!=-; i=e[i].next)
{
q=e[i].v;
if(!vis[q])
{
vis[q]=;
d[q] = d[p] + e[i].L;
Q.push(q);
if(Max<d[q])
{
Max = d[q];
Index = q;
}
}
}
}
}
int main()
{
int T, n, t=, a, b, c;
scanf("%d", &T);
while(T--)
{
memset(Head, -, sizeof(Head));
memset(d, , sizeof(d));
cnt = ;
scanf("%d", &n);
for(int i=; i<n; i++)
{
scanf("%d%d%d", &a, &b, &c);
Add(a+, b+, c);
Add(b+, a+, c);
}
Max = ; Index = -;
bfs();
bfs(Index);
printf("Case %d: %d\n", t++, Max);
}
return ;
}
Farthest Nodes in a Tree ---LightOj1094(树的直径)的更多相关文章
- lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- light oj 1094 Farthest Nodes in a Tree(树的直径模板)
1094 - Farthest Nodes in a Tree problem=1094" style="color:rgb(79,107,114)"> probl ...
- LightOJ 1094 - Farthest Nodes in a Tree(树的直径)
http://acm.hust.edu.cn/vjudge/contest/121398#problem/H 不是特别理解,今天第一次碰到这种问题.给个链接看大神的解释吧 http://www.cnb ...
- lght oj 1257 - Farthest Nodes in a Tree (II) (树dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1257 跟hdu2196一样,两次dfs //#pragma comment(l ...
- LightOJ1094 - Farthest Nodes in a Tree(树的直径)
http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycle ...
- lightoj1094 - Farthest Nodes in a Tree
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limi ...
- Farthest Nodes in a Tree (求树的直径)
题目链接,密码:hpu Description Given a tree (a connected graph with no cycles), you have to find the farthe ...
- 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. Th ...
- LightOJ 1094 - Farthest Nodes in a Tree
http://lightoj.com/volume_showproblem.php?problem=1094 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点, ...
随机推荐
- MYSQL 两日期之间的工作日(除去周六日,不考虑节假日)
select (floor(days/7)*5+days%7 -case when 6 between wd and wd+days%7-1 then 1 else 0 end - ...
- ef中用lambda expressions时要注意(m=>m.id ==b ) 此时的b只能是基本的数据类型 。连属性都不能用
ef中用lambda expressions时要注意(m=>m.id ==b ) 此时的b只能是基本的数据类型 .连属性都不能用
- ChemDraw加键的两种方法
绘制化学结构离不开9种ChemDraw键工具,键工具在绘制过程中提供了最大的使用优势,这种优势体现在键角.键长的绘制,故很有必要学习相关的ChemDraw使用技巧.本ChemDraw教程将具体介绍在C ...
- 实操演练!MathType几个绝妙小技巧!
在论文中编写公式时MathType绝对是很多人不二的选择,它的功能比较完善,操作比较方便,包含的符号模板很多,易学易上手,这些都是它的优点.但是在使用MathType时,还有很多绝妙的小技巧,使用起来 ...
- 更改VS2010的[默认开发语言]
1.菜单-->"工具"-->"导入导出设置".例如以下图: 2.选择"重置全部设置",例如以下图: 3.重置设置,例如以下图: ...
- ArcGIS ArcPy Python处理数据
1.使用搜索游标查看行中的字段值.import arcpy # Set the workspace arcpy.env.workspace = "c:/base/data.gdb" ...
- Nginx(四)-- 配置文件之location
1.location的作用 location主要做定位功能,根据uri来进行不同的定位. 2.location的语法 location [=|~|~*|^~] /uri/ { …} = 开头表示精确匹 ...
- 学习C#基础知识这段时间
似乎穿越的感觉,我又来到了这周的周五,总是在周五,知道了时间的概念,上午会讲课,但是在下午就是一个总结小练习,上午老师给我们讲了委托,在听课时间感觉很简单啊,哪里有难的地方啊,一直在好奇,老师在演示给 ...
- eclispe创建gradle项目
1.打开eclipse,选择Help——>install from Catalog,安装如图所示的gradle 2.右击空白处,new——>other——>Gradle——>G ...
- Docker 使用指南 (五)—— Dockerfile 详解
版权声明:本文由田飞雨原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/97 来源:腾云阁 https://www.qclou ...