POJ [P2631] Roads in the North
树的直径
树的直径求法:
- 任取一点u,找到树上距u最远的点s
- 找到树上距s点最远的点t,s->t的距离即为所求
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
using namespace std;
int init(){
int rv=0,fh=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') fh=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
rv=(rv<<1)+(rv<<3)+c-'0';
c=getchar();
}
return fh*rv;
}
const int MAXN=30005;
int n,m,head[MAXN],nume,q,dis[MAXN];
struct edge{
int to,nxt,dis;
}e[MAXN<<1];
void adde(int from,int to,int dis){
e[++nume].to=to;
e[nume].nxt=head[from];
head[from]=nume;
e[nume].dis=dis;
}
bool f[MAXN];
int bfs(int s){
queue<int>q;
q.push(s);
dis[s]=0;
memset(dis,0,sizeof(dis));
memset(f,0,sizeof(f));
f[s]=1;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to;
if(!f[v]){
q.push(v);
f[v]=1;
dis[v]=dis[u]+e[i].dis;
}
}
}
}
int main(){
freopen("in.txt","r",stdin);
int u,v,di;
while(~scanf("%d%d%d",&u,&v,&di)){
n=max(n,u);n=max(n,v);
adde(u,v,di);adde(v,u,di);
}
bfs(1);
int t=0,ma=0;
for(int i=1;i<=n;i++){
if(ma<dis[i]){
ma=dis[i];
t=i;
}
}
//cout<<t<<endl;
bfs(t);
ma=0;
//for(int i=1;i<=n;i++) cout<<dis[i]<<endl;
for(int i=1;i<=n;i++){
if(ma<dis[i]){
ma=dis[i];
}
}
cout<<ma;
fclose(stdin);
return 0;
}
POJ [P2631] 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 ...
- poj 2631 Roads in the North
题目连接 http://poj.org/problem?id=2631 Roads in the North Description Building and maintaining roads am ...
- 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 (自由树的直径)
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4513 Accepted: 215 ...
- 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(最长链)
Description Building and maintaining roads among communities in the far North is an expensive busine ...
- POJ 2631 Roads in the North (求树的直径)
Description Building and maintaining roads among communities in the far North is an expensive busine ...
- POJ 2631 Roads in the North (模板题)(树的直径)
<题目链接> 题目大意:求一颗带权树上任意两点的最远路径长度. 解题分析: 裸的树的直径,可由树形DP和DFS.BFS求解,下面介绍的是BFS解法. 在树上跑两遍BFS即可,第一遍BFS以 ...
- POJ 2631 Roads in the North (树的直径)
题意: 给定一棵树, 求树的直径. 分析: 两种方法: 1.两次bfs, 第一次求出最远的点, 第二次求该点的最远距离就是直径. 2.同hdu2196的第一次dfs, 求出每个节点到子树的最长距离和次 ...
随机推荐
- POJ3258-River Hopscotch-二分
这个题就是排排坐,二分就可以了... River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1325 ...
- 配置ubuntu网络
第一步:找到ubuntu中的网络标志,点击Edit connection 第二步:点击Add会出来一行配置网络的提示 第三步:选中Wired connectiong 1 然后点击Edit 第四步:选中 ...
- Spider_Man_5.2 の Mongodb_使用
一:简介 MongoDB是一款强大.灵活.且易于扩展的通用型数据库1.易用性 MongoDB是一个面向文档(document-oriented)的数据库,而不是关系型数据库.不采用关系型主要是为了获得 ...
- 改进ban冒泡排序
设置一标志性变量pos,用于记录每趟排序中最后一次进行交换的位置.由于pos位置之后的记录均已交换到位,故在进行下一趟排序时只要扫描到pos位置即可. //改进后算法如下: function bubb ...
- 科普:String hashCode 方法为什么选择数字31作为乘子
1. 背景 某天,我在写代码的时候,无意中点开了 String hashCode 方法.然后大致看了一下 hashCode 的实现,发现并不是很复杂.但是我从源码中发现了一个奇怪的数字,也就是本文的主 ...
- C#编写影院售票系统(A project with a higher amount of gold )(2:相关代码)
此篇文章为项目代码,,,需要项目需求 ,思路分析与窗体效果请访问:http://www.cnblogs.com/lsy131479/p/8367304.html 项目类图: 影院类: using Sy ...
- JSTL遇到的问题
1.jstl 中不可以用关键字命名 例如class new. 2.jstl取值的问题 如果jstl通过对象.属性取值 属性值中包括特殊字符(例如:31/20180131195356867.txt&qu ...
- linux_硬件信息
运维关注哪些方面? CPU:对计算机工作速度和效率起决定性作用(intel amd) 内存: 临时存放数据:容量和处理速度,决定数据传输快慢 硬盘(disk):数据持久化,决定电脑反应速度:优化硬盘是 ...
- python_virtualenvwrapper安装与使用
如何创建当前python版本虚拟环境? 1. 安装virtualenv pip install -i https://pipy.doubanio.com/simple virtualenv 2. 创建 ...
- junit设计模式--适配器模式
适配器(Adapter)模式 在软件系统中,由于环境的变化,常常需要将"一些现存的对象"放在新的环境中应用,但是新环境要求的接口是这些现存对象所不满足的.那么如何应对这种" ...