Farthest Nodes in a Tree ---LightOj1094(树的直径)
题目链接: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 |
Notes
Dataset is huge, use faster i/o methods.
刚读的时候就是感觉是最长璐, 后来知道就是树的直径;
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std; #define N 32200
#define INF 0xfffffff int Head[N], cnt, Max, Index, vis[N], d[N];
struct node
{
int v, L, next;
}e[N*]; void Add(int u, int v, int L)
{
e[cnt].L = L;
e[cnt].v = v;
e[cnt].next = Head[u];
Head[u] = cnt++;
}
void bfs(int u)
{
memset(vis, , sizeof(vis));
queue<int>Q;
int p, q;
d[u]=;
vis[u]=;
Q.push(u);
while(Q.size())
{
p=Q.front();
Q.pop();
for(int i=Head[p]; i!=-; i=e[i].next)
{
q=e[i].v;
if(!vis[q])
{
vis[q]=;
d[q] = d[p] + e[i].L;
Q.push(q);
if(Max<d[q])
{
Max = d[q];
Index = q;
}
}
}
}
}
int main()
{
int T, n, t=, a, b, c;
scanf("%d", &T);
while(T--)
{
memset(Head, -, sizeof(Head));
memset(d, , sizeof(d));
cnt = ;
scanf("%d", &n);
for(int i=; i<n; i++)
{
scanf("%d%d%d", &a, &b, &c);
Add(a+, b+, c);
Add(b+, a+, c);
}
Max = ; Index = -;
bfs();
bfs(Index);
printf("Case %d: %d\n", t++, Max);
}
return ;
}
Farthest Nodes in a Tree ---LightOj1094(树的直径)的更多相关文章
- 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 ...
- LightOJ 1094 - Farthest Nodes in a Tree(树的直径)
http://acm.hust.edu.cn/vjudge/contest/121398#problem/H 不是特别理解,今天第一次碰到这种问题.给个链接看大神的解释吧 http://www.cnb ...
- lght oj 1257 - Farthest Nodes in a Tree (II) (树dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1257 跟hdu2196一样,两次dfs //#pragma comment(l ...
- LightOJ1094 - Farthest Nodes in a Tree(树的直径)
http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycle ...
- 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 (求树的直径)
题目链接,密码: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找到最长路的终点, ...
随机推荐
- 页面的checkbox框的全选与反选
if (typeof jQuery == 'undefined') { alert("请先导入jQuery");} else { jQuery.extend({ ...
- 【Java面试题】10 abstract的method是否可同时是static,是否可同时是native,是否可同时是synchronized?
1.abstract是抽象的,指的是方法只有声明而没有实现,他的实现要放入声明该类的子类中实现. 2.static是静态的,是一种属于类而不属于对象的方法或者属性 3.synchronized 是同步 ...
- input文本框与图片的对齐
<input name="demo" style="width:100px;vertical-align:top" /> <img src=& ...
- ubuntu-14.04.2-desktop-amd64.iso:ubuntu-14.04.2-desktop-amd64:安装Oracle11gR2
ubuntu 桌面版的安装不介绍. 如何安装oracle:核心步骤和关键点. ln -sf /bin/bash /bin/sh ln -sf /usr/bin/basename /bin/basena ...
- TFS2010 分支问题
最近在使用TFS2010分支的时候,对项目怎么分支都无法分支. 原因:TFS只支持对文件夹分支不针对项目分支 解决:项目建一个文件夹 把项目移动进去,再进行分支即可. 提示:Nuget会出现意外的路径 ...
- linux环境中查看主机型号(机器型号)
需求说明: 今天一同事让统计测试环境主机型号,在此记录下. 操作过程: 1.通过dmidecode工具查询,产品型号(机器型号) [root@redhat6 ~]# dmidecode | grep ...
- Android程序增加代码混淆器
增加代码混淆器.主要是增加proguard-project.txt文件的规则进行混淆,之前新建Android程序是proguard.cfg文件 能够看一下我採用的通用规则(proguard-proje ...
- PHP-002
PHP URL重写 怎样在IIS环境下配置Rewrite规则_百度经验 http://jingyan.baidu.com/article/c33e3f485a7c74ea15cbb50e.html W ...
- 新唐ARM9之NUC972学习历程之系统的搭建和BSP包的使用
说到嵌入式,我们首先想到的,就是它的复杂程度,LINUX,BSP,UBOOT,交叉编译,寄存器配置,等等一系列的问题,甚至有的时候我们对此一头雾水,很是头疼,不过我们今天要说的就是关于NUC972的一 ...
- javascript中五种基本数据类型
前言: JavaScript中有五种基本数据类型(也叫做简单数据类型)分别为:undefined.null.bolean.number.string:另外还含有一种复杂的数据类型:object. 深入 ...