(树的直径)LightOJ -- 1094
链接:
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#problem/C
求树的直径,这里只不过给每条边增加一个边长属性,变成了求树上两点距离最远为多少
树的直径,在今天比赛前我是没有接触过的, 队友说在学长的博客上看是用了两个bfs就可以了,可我看的题解是用了两个dfs
应该意思是一样的吧!!! 都学习一下
题解的代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std; #define N 30005 struct node
{
int v, next, w;
}edge[N<<]; int Head[N], dis[N];
int cnt, Max, Index; void Add(int u, int v, int w)
{
edge[cnt].v = v;
edge[cnt].w = w;
edge[cnt].next = Head[u];
Head[u] = cnt++;
} void dfs(int u, int w)
{
dis[u] = w; if(dis[u] > Max)
{
Max = dis[u];
Index = u;
} for(int i=Head[u]; i!=-; i=edge[i].next)
{
int v = edge[i].v; if(dis[v] == -)
dfs(v, dis[u]+edge[i].w);
}
} int main()
{
int t, n, iCase=; scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
memset(Head, -, sizeof(Head));
cnt = Max = ; for(int i=; i<n; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
Add(u, v, w);
Add(v, u, w);
} memset(dis, -, sizeof(dis));
dfs(, ); memset(dis, -, sizeof(dis));
dfs(Index, );
printf("Case %d: %d\n", iCase++, Max);
}
return ;
}
(树的直径)LightOJ -- 1094的更多相关文章
- 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 ...
- Farthest Nodes in a Tree ---LightOj1094(树的直径)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no ...
- LightOJ 1094 - Farthest Nodes in a Tree
http://lightoj.com/volume_showproblem.php?problem=1094 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点, ...
- 【lightoj-1094】树的直径(DFS)
链接:http://www.lightoj.com/volume_showproblem.php?problem=1094 题意: 一共n各节点编号0-n-1, 输入n-1条无向边代表u-v距离为w, ...
- LightOJ1094 - Farthest Nodes in a Tree(树的直径)
http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycle ...
- poj2631 求树的直径裸题
题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...
- poj1985 Cow Marathon (求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 3195 Accepted: 1596 Case ...
- VIJOS1476旅游规划[树形DP 树的直径]
描述 W市的交通规划出现了重大问题,市政府下决心在全市的各大交通路口安排交通疏导员来疏导密集的车流.但由于人员不足,W市市长决定只在最需要安排人员的路口安放人员.具体说来,W市的交通网络十分简单,它包 ...
随机推荐
- 提取linux中eth0的IP地址
法1:cut [root@oldboy oldboy]# ifconfig eth0|grep 'inet addr'|cut -d ":" -f2|cut -d " & ...
- python-最好大学排名
# -*- coding: utf-8 -*-"""Created on Mon Apr 3 09:37:52 2017 @author: zuihaodaxuepaim ...
- Haskell语言学习笔记(50)Extra
Extra 安装 extra 模块. $ cabal install extra Installed extra-1.6 Prelude> :m +Extra Prelude Extra> ...
- js中常用的内部函数的使用
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 关于sql 增删改
1.更改数据库的名称 --更改数据库的名称,逗号前面是之前的,后面是改成的名 sp_renamedb student,xuesheng 2.表中有数据的情况下再添加列.删除列 --后来添加列,只能默认 ...
- conductor任务域
任务域 任务域有助于支持任务开发.这个想法是相同的“任务定义”可以在不同的“域”中实现.域名开发人员控制的任意名称.因此,当工作流程启动时,调用者可以在工作流中的所有任务中指定哪些任务需要在特定域中运 ...
- Spring RabbitMQ 延迟队列
一.说明 在实际业务场景中可能会用到延时消息发送,例如异步回调失败时的重发机制. RabbitMQ本身不具有延时消息队列的功能,但是可以通过rabbitmq-delayed-message-excha ...
- UnicodeEncodeError: ‘ascii’ codec can’t encode
[UnicodeEncodeError: ‘ascii’ codec can’t encode] Python默认环境编码通过下面的方法可以获取: 基本上是ascii编码方式,由此Python自然调用 ...
- 使用maven将项目热发布到tomcat7的坑
首先是配置tomcat的用户权限问题,最好是配置最大的权限,要不然会报错,我之前就是一直报错 <role rolename="manager"/> <user u ...
- 1.Two Sum (Array; HashTable)
Given an array of integers, find two numbers such that they add up to a specific target number. The ...