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. @font-face

     /**  * jQuery.hhNewSilder 滚动图片插件  * User: huanhuan  * QQ: 651471385  * Email: th.wanghuan@gmail.com ...

  2. 【转】Spring事务管理

    原文链接 在 Spring 中,事务是通过 TransactionDefinition 接口来定义的.该接口包含与事务属性有关的方法.具体如清单 1 所示: 清单 1. TransactionDefi ...

  3. CSS三角形广告文字

    街上经常碰到一些发各类广告传单的,有一次收到一张房地产广告的传单,顺手留下来,看着里面有些广告挺吸引人,同时也想练练自己css技术,故抽空做了一下. 原图某区域如下: 实现上图效果是需要一些想象力的, ...

  4. 查看文章 mysql:表注释和字段注释[转]

    1 创建表的时候写注释 create table test1 ( field_name int comment '字段的注释' )comment='表的注释'; 2 修改表的注释 alter tabl ...

  5. (转载)delphi 把图片存入数据库

    delphi 把图片存入数据库 procedure TForm1.Button1Click(Sender: TObject); // 插入图片过程var Stream:TMemoryStream;be ...

  6. 4种方法生成二维码 (js 控制canvas 画出 二维码)

    随着网络的迅速发展 发展 发展,二维码的应用将会越来越多.同时很多只是很平凡的二维码,请拿起你的手 把这个二维码 设计起来吧.下面分享了几个非常好的二维码设计.  二维码原理: 二维条码/二维码可以分 ...

  7. HTML5:一个拖拽网页元素的例子

    关键字:HTML5, Drag&Drop, JavaScript, CSS 运行环境:Chrome <!DOCTYPE html> <html> <head> ...

  8. PhoneGap 3 在 Mac 上安装使用

    1.下载安装 NodeJS . 2.安装 PhoneGap.打开终端执行: 1 $ sudo npm install -g phonegap 3.PhoneGap 3 不需要在Xcode中创建,而是在 ...

  9. aircrack-ng on OSX 从零开始之探测

    继续上一篇内容,在安装好aircrack-ng之后,就要学习如何对目标进行探测了.找了篇教程跟着学习一下吧.其实网上关于使用aircrack-ng的教程还是很多的,我也参考了很多,不过最后还是以官方的 ...

  10. 系统调用与API的区别

    整理自系统调用与API的区别 1.为什么用户程序不能直接访问系统内核模式提供的服务? 答:在linux中,将程序的运行空间分为内核与用户空间(内核态和用户态),在逻辑上它们之间是相互隔离的,因此用户程 ...