Roads in the North (树的直径)
Given is an area in the far North comprising a number of villages and roads among them such that any village can be reached by road from any other village. Your job is to find the road distance between the two most remote villages in the area.
The area has up to 10,000 villages connected by road segments. The villages are numbered from 1.
Input
Output
Sample Input
5 1 6
1 4 5
6 3 9
2 6 8
6 1 7
Sample Output
22 代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#define Inf 0x3f3f3f3f const int maxn=1e4+;
typedef long long ll;
using namespace std;
struct edge
{
int to,cost;
}; vector<edge> e[maxn];
int farthest,ans;
void dfs(int x,int pre,int dis)
{
for(int i=;i<e[x].size();i++)
{
int xx = e[x][i].to;
if(xx == pre)
continue;
dfs(xx,x,dis+e[x][i].cost);
}
if(dis > ans)
{
ans = dis;
farthest = x;
}
}
int main()
{
int i,j; int x,y;
edge t;
while(scanf("%d%d%d",&x,&y,&t.cost)!=EOF)
{
t.to = y;
e[x].push_back(t);
t.to = x;
e[y].push_back(t);
}
ans = ;
dfs(,-,); dfs(farthest,-,);
printf("%d\n",ans); return ;
}
Roads in the North (树的直径)的更多相关文章
- POJ 2631 Roads in the North(树的直径)
POJ 2631 Roads in the North(树的直径) http://poj.org/problem? id=2631 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u ...
- 【POJ2631】Roads in the North 树的直径
题目大意:给定一棵 N 个节点的边权无根树,求树的直径. 代码如下 #include <cstdio> #include <algorithm> using namespace ...
- poj--2631--Roads in the North(树的直径 裸模板)
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2389 Accepted: 117 ...
- Codeforces 1413F - Roads and Ramen(树的直径+找性质)
Codeforces 题目传送门 & 洛谷题目传送门 其实是一道还算一般的题罢--大概是最近刷长链剖分,被某道长链剖分与直径结合的题爆踩之后就点开了这题. 本题的难点就在于看出一个性质:最长路 ...
- poj 2631 Roads in the North【树的直径裸题】
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2359 Accepted: 115 ...
- POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)
题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in ...
- poj 2631 Roads in the North (自由树的直径)
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4513 Accepted: 215 ...
- POJ 2631 Roads in the North (模板题)(树的直径)
<题目链接> 题目大意:求一颗带权树上任意两点的最远路径长度. 解题分析: 裸的树的直径,可由树形DP和DFS.BFS求解,下面介绍的是BFS解法. 在树上跑两遍BFS即可,第一遍BFS以 ...
- POJ 2631 Roads in the North (树的直径)
题意: 给定一棵树, 求树的直径. 分析: 两种方法: 1.两次bfs, 第一次求出最远的点, 第二次求该点的最远距离就是直径. 2.同hdu2196的第一次dfs, 求出每个节点到子树的最长距离和次 ...
- CF835F Roads in the Kingdom/UOJ126 NOI2013 快餐店 树的直径
传送门--CF 传送门--UOJ 题目要求基环树删掉环上的一条边得到的树的直径的最小值. 如果直接考虑删哪条边最优似乎不太可做,于是考虑另一种想法:枚举删掉的边并快速地求出当前的直径. 对于环上的点, ...
随机推荐
- 自定义 Mybatis 框架
分析流程 1. 引入dom4j <dependencies> <!--<dependency> <groupId>org.mybatis</groupI ...
- 2019.12.9Java课堂总结
今天在课堂上进行了练习.现进行成果及不足汇报: 1.完成了登录界面的设计 2.完成了数据库的连接. 3.完成了数据库表的设计 4.完成了变量的定义与初始化以及get.set的设立. 5.对整体框架 ...
- Nginx MogileFS 配置
配置好MogileFS, 见mogilefs的安装与配置随笔 下载nginx.1.10.3.tar.gz, nginx_mogilefs_module.1.0.4.tar.gz 编译安装 将连个tar ...
- C#LeetCode刷题之#590-N叉树的后序遍历(N-ary Tree Postorder Traversal)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4092 访问. 给定一个 N 叉树,返回其节点值的后序遍历. 例如 ...
- C#LeetCode刷题之#575-分糖果(Distribute Candies)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3798 访问. 给定一个偶数长度的数组,其中不同的数字代表着不同种 ...
- linux手动安装python
前提:你的linux服务器必须有gcc编译器,gcc查看方法:linux命令行>gcc -v 如果返回版本信息证明已经安装, 如果找不到命令,跳到这篇手动安装gcc >>> l ...
- Android 使用AS编译出错:找不到xx/desugar/debug/66.jar (系统找不到指定的文件。)
以为是合作人配置文件的问题,后发现是缓存的问题,只需要Clean project,即可. 若提示无法删除目录:Unable to delete directory,或许是因为你打开了另一个项目,只需关 ...
- 基于vue的实时视频流开发
背景:多个实时视频的介入 技术:hls.js的流媒体,支持格式已m3u8为主 解决了什么:多个实时视频长时间播放会有卡顿的情况 具体代码实现: import Hls from 'hls.js' pla ...
- 【Net】StreamWriter.Write 的一点注意事项
背景 今天在维护一个旧项目的时候,看到一个方法把string 转换为 byte[] 用的是写入内存流的,然后ToArray(),因为平常都是用System.Text.Encoding.UTF8.Get ...
- 团队作业1——团队展示&选题(银河超级无敌舰队)
一.团队展示 1.队名: 银河超级无敌舰队 2.队员学号: 姓名 学号 郭奕材(组长) 3118004959 刘婉儿(PM) 3218004994 辜仰淦 3118004957 王煜墉 3118004 ...