Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu

Submit
Status

Description

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

2

4

0 1 20

1 2 30

2 3 50

5

0 2 20

2 1 10

0 3 29

0 4 50

Sample Output

Case 1: 100

Case 2: 80

Hint

Source

Problem Setter: Jane Alam Jan 

#include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAXN 30000+10
#define MAXM 900000+10
#define INF 0x3f3f3f
int n,cnt,head[MAXN],vis[MAXN],dis[MAXN];
int ans,sx;
struct node
{
int u,v;
int val,next;
}edge[MAXM];
void add(int u,int v,int val)
{
node E={u,v,val,head[u]};
edge[cnt]=E;
head[u]=cnt++;
}
void bfs(int x)
{
queue<int>q;
ans=0;
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
q.push(x);
vis[x]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(!vis[E.v]&&dis[E.v]<dis[u]+E.val)
{
vis[E.v]=1;
dis[E.v]=dis[u]+E.val;
q.push(E.v);
if(dis[E.v]>ans)
{
ans=dis[E.v];
sx=E.v;
}
}
}
}
}
int main()
{
int t;
int k=1;
cin>>t;
while(t--)
{
cin>>n;
cnt=0;
memset(head,-1,sizeof(head));
int a,b,c;
for(int i=1;i<n;i++)
{
scanf("%d%d%d",&a,&b,&c);
a++,b++;
add(a,b,c);
add(b,a,c);
}
sx=1;
bfs(1);
bfs(sx);
printf("Case %d: %d\n",k++,ans);
}
return 0;
}

LightOJ--1094-- Farthest Nodes in a Tree(树的直径裸题)的更多相关文章

  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. 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. A - I Wanna Be the Guy

    Problem description There is a game called "I Wanna Be the Guy", consisting of n levels. L ...

  2. HBase编程 API入门系列之delete(管理端而言)(9)

    大家,若是看过我前期的这篇博客的话,则 HBase编程 API入门之delete(客户端而言) 就知道,在这篇博文里,我是在客户端里删除HBase表的. 这里,我带领大家,学习更高级的,因为,在开发中 ...

  3. unity3d 让物体移动到点击位置

    using UnityEngine; using System.Collections; public class test : MonoBehaviour { //在场景中鼠标点击地面后,角色可以移 ...

  4. P1410 子序列

    题目描述 给定一个长度为N(N为偶数)的序列,问能否将其划分为两个长度为N/2的严格递增子序列, 输入输出格式 输入格式: 若干行,每行表示一组数据.对于每组数据,首先输入一个整数N,表示序列的长度. ...

  5. Git Learning Part II - Working locally

    file status life circle basic: modified:   Examples: untracked: unmodified: modified: Git branching ...

  6. Outlook2010规则:尝试操作失败,找不到某个对象

    可以尝试通过清除规则的方法 启动 Outlook 并删除基于客户端的规则:outlook /cleanclientrules 如果失败,再执行这句 启动 Outlook 并删除基于服务器端的规则:ou ...

  7. 工厂模式-CaffeNet训练

    参考链接:http://blog.csdn.net/lingerlanlan/article/details/32329761 RNN神经网络:http://nbviewer.ipython.org/ ...

  8. 关于 Windows 10 如何扩展分区与合并分区

    前言 相信大部分人都遇见磁盘不够用的问题吧,然后都在后悔当初为什么就给 x 盘分了 10G 的容量吧. 不过没关系,自从 Windows 7 开始( xp 我也不知道有毛有),Windows 自带的磁 ...

  9. 开发一款合格的APP成本费用大概是多少?

    随着移动互联网的发展,APP开发已经成了当下最热门的话题.无数人都盼望做出下一个微信.滴滴打车等等神奇的APP软件.如今,APP开发门槛已经非常低,媒体上也充斥着各种小团队创造奇迹的故事.不过,APP ...

  10. Mybatis批量插入的代码实现

    简单的学习总结一下,希望能帮到需要的同学! 1.mapper.xml文件sql语句如下: <insert id="insertBatch" parameterType=&qu ...