LightOJ1094 - Farthest Nodes in a Tree(树的直径)
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(树的直径)的更多相关文章
- lightoj1094 - Farthest Nodes in a Tree
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limi ...
- LightOJ--1094-- Farthest Nodes in a Tree(树的直径裸题)
Farthest Nodes in a Tree Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu S ...
- Farthest Nodes in a Tree ---LightOj1094(树的直径)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no ...
- lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- light oj 1094 Farthest Nodes in a Tree(树的直径模板)
1094 - Farthest Nodes in a Tree problem=1094" style="color:rgb(79,107,114)"> probl ...
- 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 ...
- LightOJ 1094 - Farthest Nodes in a Tree
http://lightoj.com/volume_showproblem.php?problem=1094 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点, ...
- 树上最长链 Farthest Nodes in a Tree LightOJ - 1094 && [ZJOI2007]捉迷藏 && 最长链
树上最远点对(树的直径) 做法1:树形dp 最长路一定是经过树上的某一个节点的. 因此: an1[i],an2[i]分别表示一个点向下的最长链和次长链,次长链不存在就设为0:这两者很容易求 an3[i ...
随机推荐
- Python3简明教程(十二)—— 模块
在这节我们将要学习 Python 模块相关知识.包括模块的概念和导入方法,包的概念和使用,第三方模块的介绍,命令行参数的使用等. 模块 到目前为止,我们在 Python 解释器中写的所有代码都在我们退 ...
- k近邻法(kNN)
<统计学习方法>(第二版)第3章 3 分类问题中的k近邻法 k近邻法不具有显式的学习过程. 3.1 算法(k近邻法) 根据给定的距离度量,在训练集\(T\)中找出与\(x\)最邻近的\(k ...
- 剑指offer8 旋转数组的最小数字
一种错误写法: class Solution { public: int minNumberInRotateArray(vector<int> rotateArray) { int len ...
- Eclipse 下载 开源项目 maven依赖丢失和 Deployment Assembly 丢失
周末下载了最新的jeecg的源码来瞅瞅,但是下载后发现,pom文件中定义的依赖都丢失了. 如下图 上网搜索了一下啊,发现需要先给这个项目这个项目 disable maven nature 然后再添加上 ...
- tmp note
cat file.txt > /dev/null 2>&1 丢弃错误和标准输出 systemctl isolate multi-user.target 切换回多用户命令行模式 sy ...
- sublime中项目无法添加文件夹
问题记录 mac中,使用vue init webpack project 后,在sublime中打开,但是添加新文件夹和删除,总提示没有权限, 然后用git提交吧 也不行,每次都要sudo 出现的提示 ...
- JS数组专题1️⃣ ➖ 数组扁平化
一.什么是数组扁平化 扁平化,顾名思义就是减少复杂性装饰,使其事物本身更简洁.简单,突出主题. 数组扁平化,对着上面意思套也知道了,就是将一个复杂的嵌套多层的数组,一层一层的转化为层级较少或者只有一层 ...
- 数独(深搜)(poj2726,poj3074)
数独(深搜)数据最弱版本(poj 2676) Description Sudoku is a very simple task. A square table with 9 rows and 9 co ...
- API对接中经常会出现的签名获取,这只是某一种,仅供给有需要的人参考
要求: 1.对所有传入参数(含系统参数和接口参数)按照字段名的 ASCII 码从小到大排序(字典序)后,使用 URL 键值对的格式.(即 key1=value1&key2=value2…)拼接 ...
- InnoDB INFORMATION_SCHEMA Tables about Compression
InnoDB INFORMATION_SCHEMA Tables about Compression 了解关于压缩的InnoDB INFORMATION_SCHEMA表,可以深入了解压缩的整体运行情况 ...