LightOJ--1094-- Farthest Nodes in a Tree(树的直径裸题)
Time Limit: 2000MS | Memory Limit: 32768KB | 64bit IO Format: %lld & %llu |
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
Hint
Source
#include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAXN 30000+10
#define MAXM 900000+10
#define INF 0x3f3f3f
int n,cnt,head[MAXN],vis[MAXN],dis[MAXN];
int ans,sx;
struct node
{
int u,v;
int val,next;
}edge[MAXM];
void add(int u,int v,int val)
{
node E={u,v,val,head[u]};
edge[cnt]=E;
head[u]=cnt++;
}
void bfs(int x)
{
queue<int>q;
ans=0;
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
q.push(x);
vis[x]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(!vis[E.v]&&dis[E.v]<dis[u]+E.val)
{
vis[E.v]=1;
dis[E.v]=dis[u]+E.val;
q.push(E.v);
if(dis[E.v]>ans)
{
ans=dis[E.v];
sx=E.v;
}
}
}
}
}
int main()
{
int t;
int k=1;
cin>>t;
while(t--)
{
cin>>n;
cnt=0;
memset(head,-1,sizeof(head));
int a,b,c;
for(int i=1;i<n;i++)
{
scanf("%d%d%d",&a,&b,&c);
a++,b++;
add(a,b,c);
add(b,a,c);
}
sx=1;
bfs(1);
bfs(sx);
printf("Case %d: %d\n",k++,ans);
}
return 0;
}
LightOJ--1094-- 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: ...
- LightOJ1094 - Farthest Nodes in a Tree(树的直径)
http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycle ...
- LightOJ 1094 - Farthest Nodes in a Tree
http://lightoj.com/volume_showproblem.php?problem=1094 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点, ...
- LightOJ 1094 - Farthest Nodes in a Tree(树的直径)
http://acm.hust.edu.cn/vjudge/contest/121398#problem/H 不是特别理解,今天第一次碰到这种问题.给个链接看大神的解释吧 http://www.cnb ...
- light oj 1094 Farthest Nodes in a Tree(树的直径模板)
1094 - Farthest Nodes in a Tree problem=1094" style="color:rgb(79,107,114)"> probl ...
- poj 2631 Roads in the North【树的直径裸题】
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2359 Accepted: 115 ...
- poj 1985 Cow Marathon【树的直径裸题】
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 4185 Accepted: 2118 Case ...
- poj2631 求树的直径裸题
题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...
- lightoj1094 - Farthest Nodes in a Tree
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limi ...
随机推荐
- VM虚拟机-Windows
前提:安装了vm虚拟机 一.下载win10原版镜像文件 一定要是原版,修改版的不能用. 推荐下载网址:http://www.xitongtiandi.net/win10yuanban/ 下载后放在D盘 ...
- PowerDesigner 逆向工程Non SQL Error : Could not load class com.mysql.jdbc.Driver
建立与数据库的连接. 在菜单条上,有一个Database的选择项: 选择connect…后弹出设置对话框: 在Data source里选择第三个单选按钮,即Connection profile:后,点 ...
- OnLineML:时序数据挖掘
关于时序分析: 我们跟随时间的脚步,试图解释现在.理解过去.甚至预测未来........ 原文链接:http://blog.sciencenet.cn/home.php?mod=space&u ...
- CDR教程-海报中的立体星星怎么画
在海报招贴.平面设计中有时需要凸显节日气氛,绘制一些立体星星图案作为陪衬.有人说cdr软件实现不了立体星星的制作,我想说,只有想不到,没有做不到.制作立体星星可通过cdr软件中的某些工具来实现,本案例 ...
- centos 6.10 永久修改主机名
1> 修改配置文件 vim /etc/sysconfig/network #HOSTNAME=localhost.localdomain HOSTNAME=tomcat 2> 修改ho ...
- SQL表查询
CREATE TABLE student( Sno ) NOT NULL PRIMARY KEY, Sname ) NOT NULL, Ssex ) NOT NULL, Sbirthday DATET ...
- Python笔记5----集合set
1.集合的概念:无序不重复 分为可变集合(set())和不可变集合(frozenset)两种 2.创建集合 aset=set('hello') >>aset={'h','e','l','o ...
- 原生node实现简易留言板
原生node实现简易留言板 学习node,实现一个简单的留言板小demo 1. 使用模块 http模块 创建服务 fs模块 操作读取文件 url模块 便于path操作并读取表单提交数据 art-tem ...
- hdu 4826
hdu 4826 题意 度度熊是一只喜欢探险的熊,一次偶然落进了一个 $ m * n $ 矩阵的迷宫,该迷宫只能从矩阵左上角第一个方格开始走,只有走到右上角的第一个格子才算走出迷宫,每一次只能走一格, ...
- [luogu2765 网络流24题] 魔术球问题 (dinic最大流)
传送门 题目描述 «问题描述: 假设有n根柱子,现要按下述规则在这n根柱子中依次放入编号为1,2,3,...的球. (1)每次只能在某根柱子的最上面放球. (2)在同一根柱子中,任何2个相邻球的编号之 ...