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

树的直径典型题目

树的直径是让求给你一棵树 所有节点和他们的距离然后求这棵树上距离最远的两个点之间的距离

分析:

先是任意找一个点,然后bfs找到离他最远距离的点,把这个点标记起来

然后用这个点再bfs一次找到直径

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include <vector>
#include <queue> using namespace std;
#define N 30005
int ans,Max,Index;
int head[N],vis[N],dis[N];
struct node
{
int v,f,next;
}e[N*]; void Inn()
{
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
memset(dis,,sizeof(dis));
ans=Index=;
}
void Add(int u,int v,int f)
{
e[ans].v=v;
e[ans].f=f;
e[ans].next=head[u];
head[u]=ans++;
} void bfs(int u)
{
Max=;
memset(vis,,sizeof(vis));
memset(dis,,sizeof(dis));
queue<int>Q;
Q.push(u);
vis[u]=;
while(!Q.empty())
{
int p,q;
p=Q.front();
Q.pop();
for(int i=head[p];i!=-;i=e[i].next)
{
q=e[i].v;
if(!vis[q])
{
vis[q]=;
Q.push(q);
dis[q]=dis[p]+e[i].f;
if(Max<dis[q])
{
Max=dis[q];
Index=q;
}
}
}
}
} int main()
{
int T,n,U,V,W,t=;
scanf("%d",&T);
while(T--)
{
Inn();
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d %d %d",&U,&V,&W);
Add(U,V,W);
Add(V,U,W);
}
bfs();
bfs(Index);
printf("Case %d: %d\n",t++,Max);
}
return ;
}

LightOJ1094 - Farthest Nodes in a Tree(树的直径)的更多相关文章

  1. lightoj1094 - Farthest Nodes in a Tree

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

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

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

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

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

  4. lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】

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

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

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

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

  8. LightOJ 1094 - Farthest Nodes in a Tree

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

  9. 树上最长链 Farthest Nodes in a Tree LightOJ - 1094 && [ZJOI2007]捉迷藏 && 最长链

    树上最远点对(树的直径) 做法1:树形dp 最长路一定是经过树上的某一个节点的. 因此: an1[i],an2[i]分别表示一个点向下的最长链和次长链,次长链不存在就设为0:这两者很容易求 an3[i ...

随机推荐

  1. 洛谷 P1434 滑雪

    题目描述 Michael喜欢滑雪.这并不奇怪,因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道在一个区域中最长 ...

  2. SQLite – GLOB子句

    SQLite – GLOB子句 .与LIKE不同,GLOB是大小写敏感的,它遵循语法的UNIX指定以下通配符. The asterisk sign (*) The question mark (?) ...

  3. 2015 AlBaath Collegiate Programming Contest(2月14日训练赛)

    A (By ggg): 题意:一个人还有x秒到红绿灯,这个红绿灯有g秒绿灯,y秒黄 灯,r秒红灯,问你到红绿灯的时候是什么灯.值得注意的是绿 灯变黄灯时,第g秒是黄灯了. B (By Anxdada) ...

  4. centos6上安装mysql8.0版本

    本博客是采用yum源的方式安装,非常的方便和快捷.(redhat 与centos7 等操作系统都可以采用此方法,步骤大体一致) mysql官网地址:   https://dev.mysql.com 开 ...

  5. vscode 用户代码片段 vue初始化模板 Snippet #新加入开头注释 自动生成文件名 开发日期时间等内容

    vue文件模板 模板变量 https://code.visualstudio.com/docs/editor/userdefinedsnippets#_variables vue.json { // ...

  6. python读取绝对路径的三种方式

    import pandas as pd dood_inf0=pd.read_csv("C:\\Users\\Administrator\\Desktop\\food_info.csv&quo ...

  7. 已安全化的ActiveX控件卸载时出现"DllUnregisterServer函数出错,错误代码:0x80070002"问题解决

    已安全化的ActiveX控件卸载时出现"DllUnregisterServer函数出错,错误代码:0x80070002"问题解决   情况一:当该控件未注册或者已经卸载时,你尝试卸 ...

  8. webpack、node、npm之间的关系

    webpack能够把.vue后缀名的文件打包成浏览器能够识别的js 而这个.vue文件装换需要打包器vue-loader 这个vue-loader打包器是可以从npm上面下载(npm上面有很多资源包) ...

  9. ajax 请求json数据中json对象的构造获取问题

    前端的界面中,我想通过ajax来调用写好的json数据,并调用add(data)方法进行解析,请求如下: json数据如下: { “type”:"qqq", "lat&q ...

  10. 连接远程docker内的mysql(navicat)

    拉取mysql镜像 docker pull mysql:5.6 查看mysql镜像 docker images | grep mysql 启动mysql容器 docker run -p 3306:33 ...