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 ...
随机推荐
- nginx基本配置与参数说明-【转】
#运行用户 user nobody; #启动进程,通常设置成和cpu的数量相等 worker_processes 1; #全局错误日志及PID文件 #error_log logs/error.log; ...
- short i=1;short i=i+1对或错,错的理由;short i+=1对或错,错的理由
short i=1; i=i+1 i+=1 这是一个类型高级与低级的问题,前面的C是short型的,后面的1为int型的,short型与int型的相加得到short型是不可取,因为必须将int型转换为 ...
- MongoDB系列[2]:MongoDB导入导出以及数据库备份
PS: 以下所有操作都是基于MongoDB自带的工具进行的,所以操作时一定要手动切换到Mongodb的bin目录下面,并且使用管理员权限运行命令 导出工具 mongoexport 概念: mongoD ...
- split files test
python split_upload.py /root/github/python/s3_test/test.txt dbelt dumps python download_to ...
- java算法 蓝桥杯 洗牌
问题描述 小弱T在闲暇的时候会和室友打扑克,输的人就要负责洗牌.虽然小弱T不怎么会洗牌,但是他却总是输. 渐渐地小弱T发现了一个规律:只要自己洗牌,自己就一定会输.所以小弱T认为自己洗牌不够均匀,就独 ...
- Nginx源码完全注释(9)nginx.c: ngx_get_options
本文分析 ngxin.c 中的 ngx_get_options 函数,其影响: nginx.c 中的: static ngx_uint_t ngx_show_help; static ngx_uint ...
- ubuntu mysql导出数据库及数据
mysqldump -u root -p mask_rcnn_realsense > /home/luo/mask_rcnn_realsense1.sql
- 637. Average of Levels in Binary Tree 二叉树的层次遍历再求均值
[抄题]: Given a non-empty binary tree, return the average value of the nodes on each level in the form ...
- Mongodb基于oplog恢复至任意时间
背景: 最近后端基于mongo的项目越来越多,MySQL基于冷备份+binlog可以恢复至任意时间点,那么mongo是否有同样的功能呢?经过调研发现可以通过dump+oplog可以实现粒度更细致的恢复 ...
- [Cookie] Clear Cookie
import com.eviware.soapui.impl.wsdl.support.http.HttpClientSupport def myCookies = testRunner.testCa ...