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 ...
随机推荐
- 一个Nice的生活主题博客模板
https://www.bitcron.com/ https://api.bitcron.com/ https://chopstack.com/
- Two Flowers CodeChef - TWOFL
https://vjudge.net/problem/CodeChef-TWOFL 先把颜色相同的合并成一个点,建好图,枚举要取的两种颜色(根据图中所有边决定哪些组合要枚举)即可 错误记录: 1.写了 ...
- DFS水题 URAL 1152 False Mirrors
题目传送门 /* 题意:一个圈,每个点有怪兽,每一次射击能消灭它左右和自己,剩余的每只怪兽攻击 搜索水题:sum记录剩余的攻击总和,tot记录承受的伤害,当伤害超过ans时,结束,算是剪枝吧 回溯写挫 ...
- js删除最后一个字符
在最近做一个系统,使用socket来完成后台操作,C#来完成前端操作.但是在定的协议里面,一定要用某个符号来表示传的数据结束.后台进行交互时,获取到的数据必须进行删除最后一个字符的操作. 比如我们协议 ...
- 关于line-height的理解(如何实现psd稿件上下文字距离为10px)
- 【学习笔记】深入理解js原型和闭包(0)——目录
文章转载:https://www.cnblogs.com/wangfupeng1988/p/4001284.html 说明: 本篇文章一共16篇章,外加两篇后补的和一篇自己后来添加的学习笔记,一共19 ...
- NestedScrollView嵌套RecycleView发生的小问题
1.解决方法:嵌套滑动不激活. recycleView.setNestedScrollingEnable(false); 这样做有个弊端,RecycleView的item会一次性加载完,不管是否显示, ...
- sql 删除重复数据
DELETE a FROM tbBuilding a WHERE EXISTS (SELECT 1 FROM tbBuilding b WHERE b.Province = a.Province AN ...
- SQLite -附加数据库
SQLite -附加数据库 考虑情况下当你有多个数据库可用,您希望使用其中任何一个.SQLite附加DTABASE语句用于选择一个特定的数据库,这个命令之后,所有SQLite语句将执行以下附加数据库. ...
- VBA Promming入门教程——变量的使用
数据类型 VBA中的数据类型可分为两种 示例 String Sub Main Dim s as string s = "Hello" msgbox(s) End Sub Singl ...