LightOJ 1094 - Farthest Nodes in a Tree
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的更多相关文章
- lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- LightOJ 1094 - Farthest Nodes in a Tree(树的直径)
http://acm.hust.edu.cn/vjudge/contest/121398#problem/H 不是特别理解,今天第一次碰到这种问题.给个链接看大神的解释吧 http://www.cnb ...
- light oj 1094 Farthest Nodes in a Tree(树的直径模板)
1094 - Farthest Nodes in a Tree problem=1094" style="color:rgb(79,107,114)"> probl ...
- lightoj1094 - Farthest Nodes in a Tree
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limi ...
- Farthest Nodes in a Tree ---LightOj1094(树的直径)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no ...
- LightOJ1094 - Farthest Nodes in a Tree(树的直径)
http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycle ...
- Farthest Nodes in a Tree (求树的直径)
题目链接,密码:hpu Description Given a tree (a connected graph with no cycles), you have to find the farthe ...
- 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 ...
- 树上最长链 Farthest Nodes in a Tree LightOJ - 1094 && [ZJOI2007]捉迷藏 && 最长链
树上最远点对(树的直径) 做法1:树形dp 最长路一定是经过树上的某一个节点的. 因此: an1[i],an2[i]分别表示一个点向下的最长链和次长链,次长链不存在就设为0:这两者很容易求 an3[i ...
随机推荐
- STL : 反向迭代器(Reverse Iterator)
1. 定义反向迭代器(Reverse Iterator)是一种反向遍历容器的迭代器.也就是,从最后一个元素到第一个元素遍历容器.反向迭代器将自增(和自减)的含义反过来了:对于反向迭代器,++运算将访问 ...
- Git(四):Git远程操作详解
转: http://www.ruanyifeng.com/blog/2014/06/git_remote.html Git是目前最流行的版本管理系统,学会Git几乎成了开发者的必备技能. Git有很多 ...
- python开源项目Scrapy抓取文件乱码解决
scrapy进行页面抓去的时候,保存的文件出现乱码,经过分析是编码的原因,只需要把编码转换为utf-8即可,代码片段 ...... import chardet ...... cont ...
- aop中的顾问
通知只能指定织入的时间点,目标方法之前,之后,环绕,还是异常时. 要想指定切入点就要使用顾问
- 网络编程基础之粘包现象与UDP协议
一.粘包现象原理分析 1.我们先来看几行代码,从现象来分析: 测试程序分为两部分,分别是服务端和客户端 服务端.py #!/usr/bin/env python3 #-*- coding:utf-8 ...
- HRESULT:0x80070057 (E_INVALIDARG)
笔记本蓝屏后,在vs2010中调试项目时出现该异常, 解决方法:清空C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Fi ...
- Python 生成MYSQL inser语句
背景: 一般来说,navicat生成的insert已经够用了 如果说一张表有2,30个字段,每个字段还得一一对上,其实是很难的.所以抽空写了个小程序.用它完全不用担心字段对不上了.因为没有时间,需要手 ...
- iOS-Runtime字体适配
你还在为适配字体大小发愁? 看这里: #define MyUIScreen 375 //UI设计原型图的手机尺寸宽度(6), 6p的--414 @implementation UIFont (Run ...
- SQL Compare数据库版本比较工具
Red Gate系列文章: Red Gate系列之一 SQL Compare 10.4.8.87 Edition 数据库比较工具 完全破解+使用教程 Red Gate系列之二 SQL Source C ...
- ChainOfResponsibilityPattern(23种设计模式之一)
参考书籍:设计模式-可复用面向对象软件基础(黑皮书) 目的:使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系.将这些对象连成一条链,并沿这条链传递该请求,直到有一个对象处理它为止 ...