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找到最长路的终点, ...
随机推荐
- 10种canvas鼠标光标动画特效
来源:http://www.sucaihuo.com/js/1780.html demo:http://www.sucaihuo.com/jquery/17/1780/demo/
- get 方法向后台提交中文乱码问题
前端js代码 function searchAll(){ var contentStr = $('#contentStr_id').val(); contentStr =encod ...
- NHibernate初学五之关联一对多关系
1:创建两张表T_Country.T_Person:其中T_Person表中有一个CountryID对应T_Country的ID,一个Country可以对应多个Person CREATE TABLE ...
- ADO.net方法
using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; usin ...
- JavaScript------生成Guid方法
转载: http://blog.csdn.net/limm33/article/details/51536529 代码: function newGuid() { var guid = "& ...
- linux命令在文件中根据命令查找
find . -type f -name "*.tmp" | xargs grep -ri "2016-08-30 04:00:00|2016-08-30 05:00:0 ...
- Python 入门(七)函数
什么是函数 我们知道圆的面积计算公式为: S = πr² 当我们知道半径r的值时,就可以根据公式计算出面积.假设我们需要计算3个不同大小的圆的面积: r1 = 12.34 r2 = 9.08 r3 = ...
- ScrollView拉到尽头时出现阴影的解决方法
<code class="hljs markdown has-numbering" style="display: block; padding: 0px; col ...
- Sublime Text 快捷键使用
Sublime Text 2包含了大量快捷操作,而且还很方便修改和追加自己喜欢的快捷键.查看快捷键的方式也很简单:------------------------------------------- ...
- 《C++ Primer Plus》第7章 函数——C++的编程模块 学习笔记
函数是C++的编程模块.要使用函数,必须提供定义和原型,并调用该函数.函数定义是实现函数功能的代码:函数原型描述了函数的接口:传递给函数的值的书目和种类以及函数的返回类型.函数调用使得程序将参数传递给 ...