树的直径

树的直径求法:

  1. 任取一点u,找到树上距u最远的点s
  2. 找到树上距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的更多相关文章

  1. POJ 2631 Roads in the North(树的直径)

    POJ 2631 Roads in the North(树的直径) http://poj.org/problem? id=2631 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u ...

  2. poj 2631 Roads in the North

    题目连接 http://poj.org/problem?id=2631 Roads in the North Description Building and maintaining roads am ...

  3. poj 2631 Roads in the North【树的直径裸题】

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2359   Accepted: 115 ...

  4. poj 2631 Roads in the North (自由树的直径)

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4513   Accepted: 215 ...

  5. POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)

    题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in ...

  6. 题解报告:poj 2631 Roads in the North(最长链)

    Description Building and maintaining roads among communities in the far North is an expensive busine ...

  7. POJ 2631 Roads in the North (求树的直径)

    Description Building and maintaining roads among communities in the far North is an expensive busine ...

  8. POJ 2631 Roads in the North (模板题)(树的直径)

    <题目链接> 题目大意:求一颗带权树上任意两点的最远路径长度. 解题分析: 裸的树的直径,可由树形DP和DFS.BFS求解,下面介绍的是BFS解法. 在树上跑两遍BFS即可,第一遍BFS以 ...

  9. POJ 2631 Roads in the North (树的直径)

    题意: 给定一棵树, 求树的直径. 分析: 两种方法: 1.两次bfs, 第一次求出最远的点, 第二次求该点的最远距离就是直径. 2.同hdu2196的第一次dfs, 求出每个节点到子树的最长距离和次 ...

随机推荐

  1. HDU--2021

    发工资咯:) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  2. c语言基础学习01

    =============================================================================每一种语言都有其独特的语法规则与语言定义. 指 ...

  3. 用.net中的SqlBulkCopy类批量复制数据 (转载)

    在软件开发中,把数据从一个地方复制到另一个地方是一个普遍的应用. 在很多不同的场合都会执行这个操作,包括旧系统到新系统的移植,从不同的数据库备份数据和收集数据. .NET 2.0有一个SqlBulkC ...

  4. 人生苦短,我用Python 第一篇

    一.变量名 自定义变量名只能用 数字.字母.下划线组成. 使用变量前,需对其先赋值 注意:1.变量名不能以数字开头:      2.不能是关键字:例如:'and', 'as', 'assert', ' ...

  5. .23-浅析webpack源码之事件流compilation(1)

    正式开始跑编译,依次解析,首先是: compiler.apply( new JsonpTemplatePlugin(options.output), // start new FunctionModu ...

  6. Arrays类详解

    数组是数据结构中最简单的一种类型.在平常的使用上也比较多见.今天就来总结一下数组在使用过程中的一些心得 1.java中包装数组的一些基本用法的抽象类  java.util.Arrays.这个类中包含操 ...

  7. 【Jsp/Servlet】获取客户端使用的ip

    一般使用jsp的时候大多数时间都可以使用request.getRemoteAddr() 来获取ip,但是这个前提是未经过反向代理等操作的原始地址,所以,需要在反向代理等操作之后还要获取客户端的ip变得 ...

  8. nxlog4go 简介 - 基于log4go的下一代go语言日志系统

    nxlog4go的项目网址: https://github.com/ccpaging/nxlog4go 项目历史 ccpaging's log4go forked from https://githu ...

  9. Angular 4+ Http

    HTTP: 使应用能够对远端服务器发起相应的Http调用: 你要知道: HttpModule并不是Angular的核心模块,它是Angualr用来进行Web访问的一种可选方式,并位于一个名叫@angu ...

  10. Visio绘制用例图问题集锦

    1.Visio画UML用例图没有include关系的解决方法 发现Visio UML用例里面找不到include关系,即"箭头"+"<<include> ...