题目链接: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(树的直径)的更多相关文章

  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. light oj 1094 Farthest Nodes in a Tree(树的直径模板)

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

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

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

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

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

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

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

  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. Farthest Nodes in a Tree (求树的直径)

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

  8. 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 ...

  9. LightOJ 1094 - Farthest Nodes in a Tree

    http://lightoj.com/volume_showproblem.php?problem=1094 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点, ...

随机推荐

  1. 【转载】关于quartus ii软件中注释乱码问题的解决方法

    最近在看Verilog代码,由于我的quartus版本打开他们的文件注释会全部乱码,痛苦万分!从网上找了下原因,解决方法基本没有,不过看到有人提出是编码的问题,立马我就想到一个解决方法,经过实验果然有 ...

  2. 基于bootstrap的Dialog

    function yms_Dialog(container_id, modal_path, handle_function) {     /// <summary>    ///      ...

  3. Linux(Ubuntu)下搭建ASP.NET Core环境

    今天来学习一下ASP.NET Core 运行在Ubuntu中.无需安装mono . 环境 Ubuntu 14.04.4 LTS 服务器版 全新安装系统. 下载地址:http://mirrors.neu ...

  4. [转]NMON服务器监控、指标说明

    一.NMON中的各项参数指标: SYS_SUMM:显示当前服务器的总体性能情况 Total System I/OStatistics:Avg tps during an interval:显示采集间隔 ...

  5. 几个shell程序设计小知识(shell常识部分)

    [转自]http://blog.chinaunix.net/uid-168249-id-2860686.html 引用:一.用户登陆进入系统后的系统环境变量:  $HOME 使用者自己的目录  $PA ...

  6. 一个牛人给Java初学者的建议

    学习Java的同学注意了!!! 学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Java学习交流群,群号码:618528494  我们一起学Java! 给初学者之一:浅谈Java及应用学java ...

  7. can not connect to MySQL server on "10.30.48.153"(13)

    国庆节前好好的程序,完了回来愣是不能跑了! 真是纳闷了,而且邮件别人都发出去了,等于这跟别人一边使用一遍救火一样子了.     查了一下午,一直以为是机器mysql服务的问题,或者是我代码的问题.该找 ...

  8. Mockito单元测试框架学习

    基本使用方法: http://zhongl.iteye.com/blog/296136 一.问题:如何将mock的类自动注入到待测类,特别是在没有setter方法的情况下. 解答: 前提:待测的ser ...

  9. mongodb启动时报错ERROR: child process failed, exited with error number 1

    不多说,直接上干货! 前期博客 Ubuntu14.04下Mongodb安装部署步骤(图文详解) Ubuntu16.04下Mongodb安装部署步骤(图文详解) root@zhouls-virtual- ...

  10. JQuery------jQuery.parseHTML()的使用方法

    代码: $(document).ready(function () { var data = jQuery.parseHTML("<p>你好</p>"); ...