http://lightoj.com/volume_showproblem.php?problem=1094

树的直径是指树的最长简单路。

求法: 两遍BFS :先任选一个起点BFS找到最长路的终点,再从终点进行BFS,则第二次BFS找到的最长路即为树的直径;

原理: 设起点为u,第一次BFS找到的终点v一定是树的直径的一个端点

证明:

1) 如果u 是直径上的点,则v显然是直径的终点(因为如果v不是的话,则必定存在另一个点w使得u到w的距离更长,则于BFS找到了v矛盾)
2) 如果u不是直径上的点,则u到v必然于树的直径相交(反证),那么交点到v 必然就是直径的后半段了
所以v一定是直径的一个端点,所以从v进行BFS得到的一定是直径长度

详细证明请参考:

http://www.cnblogs.com/wuyiqi/archive/2012/04/08/2437424.html

#include <cstdio>
#include <cstring>
#include <ostream>
#include <algorithm> using namespace std; #define N 30010 struct Edge
{
int u, v, next, l;
}edge[N * ]; int head[N], dist[N], Max, cnt, Index; void Init()
{
memset(head, -, sizeof(head));
cnt = ;
} void AddEdge(int u, int v, int l)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].l = l;
edge[cnt].next = head[u];
head[u] = cnt++;
} void DFS(int u, int l)
{
int v, i;
dist[u] = l;
if(dist[u] > Max)
{
Max = dist[u];
Index = u;
}
for(i = head[u] ; i != - ; i = edge[i].next)
{
v = edge[i].v;
if(dist[v] == -)
DFS(v, edge[i].l + dist[u]);
}
} int main()
{
int t, n, u, v, l, i, x = ;
scanf("%d", &t);
while(t--)
{
x++;
Init();
scanf("%d", &n);
for(i = ; i < n ; i++)
{
scanf("%d%d%d", &u, &v, &l);
AddEdge(u, v, l);
AddEdge(v, u, l);
}
Max = ;
memset(dist, -, sizeof(dist));
DFS(, );//以树中任意一个结点为源点(这里暂且选0当源点),进行一次广度优先遍历,找出离源点距离最远的点Index
memset(dist, -, sizeof(dist));
DFS(Index, );//以Index为源点,进行一次广度优先遍历,找出离Index最远的点,并记录其长度,该长度即为树的直径
printf("Case %d: %d\n", x, Max);
}
return ;
}

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

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

  3. light oj 1094 Farthest Nodes in a Tree(树的直径模板)

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

  4. lightoj1094 - Farthest Nodes in a Tree

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

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

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

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

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

  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. 树上最长链 Farthest Nodes in a Tree LightOJ - 1094 && [ZJOI2007]捉迷藏 && 最长链

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

随机推荐

  1. 【源码阅读】Java集合之一 - ArrayList源码深度解读

    Java 源码阅读的第一步是Collection框架源码,这也是面试基础中的基础: 针对Collection的源码阅读写一个系列的文章,从ArrayList开始第一篇. ---@pdai JDK版本 ...

  2. 跟着太白老师学python day10 名称空间,作用域和取值顺序,变量的加载顺序

    名称空间分为3种: 1. 全局名称空间 2. 内置名称空间 3. 局部名称空间(临时) 作用域 全局作用域              1全局名称空间 2 内置名称空间 局部作用域           ...

  3. ffmpeg打开视频解码器失败:Could not find codec parameters for stream 0 (Video: h264): unspecified size

    在使用ffmpeg进行拉流分离音视频数据再解码播放操作的时候: 有时候经常会报错: Could not find codec parameters for stream 0 (Video: h264) ...

  4. 工具类: 用于模拟HTTP请求中GET/POST方式

    package com.jarvis.base.util; import java.io.BufferedReader; import java.io.IOException; import java ...

  5. C++11之 auto

    [C++11类型推导] 1.使用auto的时候,编译器根据上下文情况,确定auto变量的真正类型.auto在C++14中可以作为函数的返回值,因此auto AddTest(int a, int b)的 ...

  6. OpenSceneGraph3.4.0+Qt5.6.1MinGW开发环境部署

                基本步骤如下描述: Step1:CMake3.10编译openscenegraph3.4.0,生成makefile文件(中间过程可能会涉及到很多三方库,需要下载编译,然后按cm ...

  7. loadrunner中回放log看不到参数替代后具体数值

    1.打开run-time settings,找到 log - always send messages,选择 extended log--parameter substitution.

  8. [OS] 如何在远程机器上用ctrl+alt+del键修改登录用户的密码

    远程登录某台机器,想修改当前登录用户的密码,系统提示按Ctrl+Alt+Del,在出现的界面里修改密码 但我一按这三个键,是在我本地客户机生效,而不是在远程服务器. 答案 : 向远程桌面发送Ctrl+ ...

  9. Mysql中结果集(mysql_result)与Traversable

    对于MySQL应该也不是很陌生吧,我常常爱犯的以错误就是执行mysli_qurey()后就使用数据,忘记返回的是结果集了.而对于lSELECT,.SHOW, DESCRIBE . EXPLAINmys ...

  10. Perl 学习笔记-文件测试

    1.文件测试操作符 针对文件和目录的测试操作符: -r/-w/-x/-o: 文件或目录, 对有效的(负责运行这个程序的)用户或组来说是可读/写/执行/拥有 的; 这些测试位会查看文件的权限位, 以此判 ...