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. jaegeropentracing的Java-client

    关于jaegeropentracing的Java-client做记录如下: 1.依赖jar包 <!-- 以下jar包是jaegeropentracing依赖的日志jar -->slf4j- ...

  2. LightGBM

    1.简介 lightGBM包含两个关键点:light即轻量级,GBM 梯度提升机 LightGBM 是一个梯度 boosting 框架,使用基于学习算法的决策树.它可以说是分布式的,高效的,有以下优势 ...

  3. #define中 #与##的神奇用法

    本文整理自csdn. #define f(a,b) a##b  #define d(a) #a  #define s(a) d(a)  void main( void )  {      puts(d ...

  4. 插入后获取到id

    第一种方法: insert INTO student(name) VALUES("南亚");SELECT @@identity 第二种方法: insert INTO student ...

  5. Creating Cubemaps in Unity3D

    [Creating Cubemaps in Unity3D] 1.在Editor目录下生成GenerateStaticCubemap.cs. 2.编写代码,生成一个继承于ScriptableWizar ...

  6. fsync性能问题

    最近在测试种发现程序里调用fsync刷文件到磁盘时,开销只有几百微秒,于是对fsync相关机制进行了一番调查. 磁盘(或RAID卡)自身通常会有硬件缓存机制,对于写操作,有write back和wri ...

  7. lock free queues

    无锁队列,下面链接是源码,包含4种队列:单生产者单消费者/多生产者多消费者,队列定长/不定长.元素建议为简单数据类型,复杂类型都采用指针形式. queues-master.zip 源码来源:https ...

  8. 【hdu6148】Valley Numer【数位dp模板题】

    题意 对于每组数据给出一个整数n(length(n)<=100),找出不大于n的数字中有多少是Valley Numer.对于Valley的定义是它每一位的数字要么是递增,要么是递减,要么是先递减 ...

  9. 【BZOJ 2120】数颜色【分块/莫队】

    题意 给出n个数字和m个操作.操作有两种.1:查询区间[l,r]内不同种类得数字个数.2: 将下标为p得数字修改为v 分析 如果不是修改操作的话,用莫队贼简单就可以水过,但是因为带了修改就有一些麻烦了 ...

  10. poj2796 Feel good

    题目给出N个数,找出一段区间使得区间最小值乘区间和的值最大 其中N<=100000 分析: 单调队列(单调栈) 求出每个值作为最小值时最长的影响区间,然后枚举判断 这找出最长影响区间应该算是单调 ...