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 ...
随机推荐
- Java基础2一基础语法
1.标识符 定义:在Java中给类名.方法名.包名,参数名等命名时使用的字符序列即标识符 规则: 由字母.数字.下划线和$符组成 不能以数字开头 长度无限制 严格区分大小写 不能是java中的保留关键 ...
- class A<T> where T:new()相关知识点
来源:http://www.cnblogs.com/FredWang/p/4284251.html class A<T> where T:new() ===>>> ...
- 使用光盘作为yum源安装ifconfig等网络命令
# mkdir -p /mnt/cdrom# 如果是光驱:mount -t iso9660 /dev/cdrom /mnt/cdrom/# 如果是ISO:mount -o loop /usr/loca ...
- vue1 到 vue2 v-el变成ref
vue1的写法 div class="menu-wrapper" v-el="menu-wrapper"> <ul class="menu ...
- ANN:DNN结构演进History—LSTM_NN
前言 语音识别和动作识别(Action.Activities) 等一些时序问题,通过微分方式可以视为模式识别方法中的变长模式识别问题.语音识别的基元为音素.音节,字母和句子模式是在时间轴上的变长序列 ...
- @RestController无法自动注入的问题
今天在练习spring boot的时候,发现在ide中无法将@RestController注入到代码中,@RestController注解依赖的包是org.springframework.web,检 ...
- 04--深入探讨C++中的引用
深入探讨C++中的引用 引用是C++引入的新语言特性,是C++常用的一个重要内容之一,正确.灵活地使用引用,可以使程序简洁.高效.我在工作中发现,许多人使用它仅仅是想当然,在某些微 ...
- SweetAlert详解
官方给出的SweetAlert介绍是:SweetAlert可以替代JavaScript原生的alert和confirm等函数呈现的弹出提示框,它将提示框进行了美化,并且允许自定义,支持设置提示框标题. ...
- C#模拟按键
try { System.Threading.Thread.Sleep(); ; i < ; i++) { SendKeys.SendWait("{ENTER}"); Sen ...
- 动态给某一个元素添加active
<li class="one_data" data-id='+ navGroup.self_first_nav[i].id +'><a href='+ navG ...