Farthest Nodes in a Tree (求树的直径)
Description
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
2
4
0 1 20
1 2 30
2 3 50
5
0 2 20
2 1 10
0 3 29
0 4 50
Sample Output
Case 1: 100
Case 2: 80

#include<cstdio>
#include<string.h>
#include<algorithm>
#define M 30010
#include<queue>
using namespace std;
int a,b,c,head[M],ans,flag[M],sum[M],node,num,i,n;
/* head表示每个节点的头“指针”
num表示总边数
ans记录最后的结果
flag[]标记访问过的节点
sum[]表示以该节点结尾的最长路
*/ struct stu
{
int from,to,val,next;
}st[M*];
void add_edge(int u,int v,int w)
{
st[num].from=u;
st[num].to=v;
st[num].val=w;
st[num].next=head[u];
head[u]=num++;
}
void bfs(int fir)
{
int u;
queue<int> que;
memset(flag,,sizeof(flag));
memset(sum,,sizeof(sum));
flag[fir]=;
que.push(fir);
ans=;
while(!que.empty())
{
u=que.front();
que.pop();
for(i = head[u] ; i != - ; i = st[i].next)
{
if(!flag[st[i].to] && sum[st[i].to] < sum[u] + st[i].val)
{
sum[st[i].to]=sum[u]+st[i].val;
flag[st[i].to]=;
if(ans < sum[st[i].to])
{
ans=sum[st[i].to];
node=st[i].to; //记录以fir为起点的最长路的端点
}
que.push(st[i].to);
}
} }
}
int main()
{
int k=;
int t;
scanf("%d",&t);
while(t--)
{
num=;
memset(head,-,sizeof(head));
scanf("%d",&n);
for(i = ; i < n ; i++)
{
scanf("%d %d %d",&a,&b,&c);
add_edge(a,b,c);
add_edge(b,a,c);
}
bfs();
bfs(node);
printf("Case %d: %d\n",++k,ans);
}
}
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: ...
- 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 ...
- 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
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limi ...
- 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 ...
- poj1985 Cow Marathon (求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 3195 Accepted: 1596 Case ...
随机推荐
- JSP | 基础 | 加载类失败:com.mysql.jdbc.Driver
两个原因: 1. 连接数据库需要的jar包没有导入Tomcat的lib库中 解决方案: 打开Tomcat的安装目录下的lib文件夹,把jar包拖进lib库后,重启tomcat服务器 2. mysql ...
- Educational Codeforces Round 18 D
Description T is a complete binary tree consisting of n vertices. It means that exactly one vertex i ...
- 线段树(单点更新) HDOJ 4288 Coder
题目传送门 #include <cstdio> #include <cstring> #define lson l, m, rt << 1 #define rson ...
- 142 Linked List Cycle II 环形链表 II
给一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null.说明:不应修改给定的链表.补充:你是否可以不用额外空间解决此题?详见:https://leetcode.com/proble ...
- HAL之EXIT
在STM32cubeMX中 1 在GPIO管脚上选定EXIT功能 2 在GPIO模式中设定触发边沿类型 3 在NVIC中设定NVIC分组及使能EIXT_Line0_interrupt 在MDK中的GP ...
- RAID 0、1、5、1+0总结
RAID(Redundant Array Of Independent Disk,独立磁盘冗余阵列),可以提供比普通磁盘更快的速度.更高的安全性,生产环境中服务器在安装时一般都会做RAID,RAID的 ...
- CSS3实现单行、多行文本溢出(省略号的形式出现)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 在proe模型文件里面存储用户数据
存储外部数据 author:visualsan 2014.2 上海 1.简介 利用外部数据存储外部接口,可以在模型文件里面尺寸用户自定义数据.在模型保存时数据自动存储,在模型载入时数据自动载入.外部数 ...
- python中*号和**号的用法
1.乘法符号 2.可变长参数 当我们使用函数时,需要传入不定个数的位置参数时,就可以使用*号表示,即*args,以元组形式传入:需要传入不定个数的关键字参数时,使用**表示,即**kwargs,以字典 ...
- 屏幕卫士模式系统APP开发
利用php的socket编程来直接给接口发送数据来模拟post的操作,(黎灿:I8O..2853..296O 可电可V)线上线下和物流结合在一起,才会产生新零售. 2016年阿里云栖大会上,阿里巴巴马 ...