1094 - Farthest Nodes in a Tree
Time Limit: 2 second(s) Memory Limit: 32 MB

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-1lines 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>
#define MAX 40010
using namespace std;
int head[MAX];
int ans=0,n,m,beg;
int sum;
int dis[MAX],vis[MAX];
struct node
{
int u,v,w;
int next;
}edge[MAX];
void add(int u,int v,int w)
{
edge[ans].u=u;
edge[ans].v=v;
edge[ans].w=w;
edge[ans].next=head[u];
head[u]=ans++;
}
void bfs(int x)
{
queue<int>q;
int i,j;
memset(dis,0,sizeof(dis));
memset(vis,0,sizeof(vis));
while(!q.empty())
q.pop();
beg=x;
vis[beg]=1;
sum=0;
q.push(beg);
int top;
while(!q.empty())
{
top=q.front();
q.pop();
for(i=head[top];i!=-1;i=edge[i].next)
{
int z=edge[i].v;
if(!vis[z])
{
dis[z]=dis[top]+edge[i].w;
vis[z]=1;
q.push(z);
if(sum<dis[z])
{
sum=dis[z];
beg=z;
}
}
}
}
}
int main()
{
int i,j,t;
int a,b,c;
scanf("%d",&t);
while(t--)
{
memset(head,-1,sizeof(head));
scanf("%d",&n);
for(i=1;i<n;i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
bfs(0);
bfs(beg);
printf("%d\n",sum);
}
return 0;
}

  

lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】的更多相关文章

  1. LightOJ--1094-- Farthest Nodes in a Tree(树的直径裸题)

    Farthest Nodes in a Tree Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu S ...

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

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

  3. LightOJ 1094 - Farthest Nodes in a Tree

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

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

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

  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. poj 2631 Roads in the North【树的直径裸题】

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2359   Accepted: 115 ...

  7. poj 1985 Cow Marathon【树的直径裸题】

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4185   Accepted: 2118 Case ...

  8. poj2631 求树的直径裸题

    题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...

  9. lightoj1094 - Farthest Nodes in a Tree

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

随机推荐

  1. Java Servlet 回顾

    一.转发请求RequestDispatcher 使用request域对象把数据带给转发资源,与重定向的区别:客户端只发出一次请求,服务器端调用多个资源,客户端浏览器地址栏没改变:转发是一次请求,使用的 ...

  2. BFC与hasLayout之间的故事

    刚拒绝了一个很有诱惑的公司,不是不想去,而是对现在的能力还不确定,希望能够进一步提高自己的技能,所有想写博客了,监督自己的学习进度·········现在还没有开放博客,希望成熟一些后再开放吧! 进入正 ...

  3. eval(phpcode) 字符当代码执行

    eval(phpcode)eval() 函数把字符串按照 PHP 代码来计算.相当于在字符串两边分别加上PHP语 法标签 该字符串必须是合法的 PHP 代码,且必须以分号结尾. 如果没有在代码字符串中 ...

  4. mysqli 扩展库的预处理技术(mysqli_stmt)

    提出问题 现在需要向mysql数据库中添加100个用户,请问如何实现? 方法一:for循环100次 方法二:使用批量添加 $sqls="insert xxx"; $sqls.=&q ...

  5. 解决 nginx 返回数据不完整的方法

    通过PHP请求接口时发现接口的内容输出没有完整的返回整个数据,早上只修改了nginx api_metrics插件里的计算response大小的代码,观察日志发现一条: 2012/08/28 02:13 ...

  6. 在Laravel5.* 中使用 AdminLTE

    在Laravel5.* 中使用 AdminLTE AdminLTE是一个很棒的单纯的由 HTML 和 CSS 构建的后台模板,在这片文章中,我将讲述如何将 AdminLTE 和 Laravel 优雅的 ...

  7. Building Python 2.7.10 with Visual Studio 2010 or 2015 - Google Chrome

    您的浏览器(Chrome 33) 需要更新.该浏览器有诸多安全漏洞,无法显示本网站的所有功能. 了解如何更新浏览器 × p-nand-q.com C++  Python  Programming  L ...

  8. WPF中将四个数字字符串值(比如:"10,10,300,300")转为Rect

    RectConverter rectConverter = new RectConverter(); string parseString = viewportEntry.Text; if (pars ...

  9. [简历] PHP 技能关键字列表

    本技能关键字列表是从最近招聘PHP的数百份JD中统计出来的,括号中是出现的词频.如果你的简历要投递给有机器(简历分选系统)和不如机器(不懂技术的HR)筛选简历环节的地方,请一定从下边高频关键词中选择5 ...

  10. Newtonsoft post Json 日期格式处理

     Newtonsoft.Json.Converters.IsoDateTimeConverter DateTimeConverter = new Newtonsoft.Json.Converters. ...