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 ...
随机推荐
- Understanding Scroll Views 深入理解 scroll view 读书笔记
Understanding Scroll Views 深入理解 scroll view 读书笔记 It may be hard to believe, but a UIScrollView is ...
- ios项目中引用其他开源项目
1. 将开源项目的.xcodeproj拖入项目frameworks 2. Build Phases下 Links Binary With Libraries 引入.a文件.Target Depende ...
- 如何在Ubuntu里安装Helm
Helm是什么?在战网上玩过暗黑破坏神2代的程序员们应该还记得,Helm是国度的意思. 而在计算机领域,Helm是什么? Helm是Kubernetes的一个包管理工具,有点像nodejs的npm,U ...
- vue之placeholder中引用字体图标
先说一下问题:在placeholder中想使用字体图标,结果渲染不正确,代码如图 效果如图 在网上get到了解决方法: 在VUE组件中,给placeholder添加图标,需要注意以下几点: 1.不要给 ...
- PHP20 PHP面向对象辅助
学习要点 常用函数 命名空间 类的自动加载 常用函数 class_exists 作用:检查类是否已定义 语法格式: bool class_exists ( string $class_name [, ...
- No-9.函数基础
函数基础 目标 函数的快速体验 函数的基本使用 函数的参数 函数的返回值 函数的嵌套调用 在模块中定义函数 01. 函数的快速体验 1.1 快速体验 所谓函数,就是把 具有独立功能的代码块 组织为一个 ...
- scrapy example
scrapy example scrapy with pycharm import win32api 出现ImportError: DLL load failed 错误的解决方法 pip instal ...
- 5.1 qbxt 一测 T1
禁咒检验 (3MB / 2s)[问题描述] 在古老的世界里,有一个神奇的职业叫做魔法师. 魔法师的特点是会魔法,施放魔法需要念咒语. 在古老的世界里,有一个神奇的职业叫做码农.码农的工作是帮助魔法师记 ...
- Luogu-P1020(导弹拦截)(DP,LIS ,二分优化)
Luogu-P1020(导弹拦截)(DP) 题意: 给n(n<=100000) 个数字,求最长不上升子序列的长度和最少的不上升子序列的个数. 分析: 第一问: 求最长不上升子序列有 O(n^2) ...
- 为什么JavaScript里面0.1+0.2 === 0.3是false
以下这一篇说明的很详细:彻底理解0.1 + 0.2 === 0.30000000000000004的背后 0.1+0.2 === 0.3 //返回是false, 这是为什么呢?? 我们知道浮点数计算是 ...