树的直径

树的直径求法:

  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. GSS4 - Can you answer these queries IV(线段树懒操作)

    GSS4 - Can you answer these queries IV(线段树懒操作) 标签: 线段树 题目链接 Description recursion有一个正整数序列a[n].现在recu ...

  2. hdu_1045Fire Net(二分图匹配)

    hdu_1045Fire Net(二分图匹配) 标签: 图论 二分图匹配 题目链接 Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...

  3. c语言基础学习08_内存管理

    =============================================================================涉及到的知识点有:一.内存管理.作用域.自动变 ...

  4. CentOS6下安装git

    Ubuntu12.04中默认没有安装Git.需要自行安装. 1. 安装Git 1.1 Ubuntu12.04下 可以使用apt-get方式安装,也可以下载源代码安装[1],我们这里使用apt-git安 ...

  5. [国嵌攻略][157][SPI总线介绍]

    SPI总线架构 SPI(serial peripheral interface)串行外设接口,是一种高速,全双工,同步的通信总线.采用主从模式(master slave)架构,支持多个slave,一般 ...

  6. Vs自定义设置

    1.固定选项卡独立行显示设置 效果如下 2.语言设置 可以从官网寻找所需版本语言包 https://my.visualstudio.com/downloads 3.代码段设置 可以对现有的进行一些改造 ...

  7. insertBefore方法(javascript与jQuery)

    说到insertBefore()方法,其实javascript与jQuery中都有此方法,那么他们用法是否相同呢? 其实,还是有点区别的.反正我是爱搞混淆了,先做个小笔记吧! 1.insertBefo ...

  8. JavaScript八张思维导图—字符串用法

    JS基本概念 JS操作符 JS基本语句 JS数组用法 Date用法 JS字符串用法 JS编程风格 JS编程实践 不知不觉做前端已经五年多了,无论是从最初的jQuery还是现在火热的Angular,Vu ...

  9. Spark算子--partitionBy

    转载请标明出处http://www.cnblogs.com/haozhengfei/p/923b11fce561e82748baa016bcfb8421.html partitionBy--Trans ...

  10. phpstudy php5.4以上版本伪静态设置 thinkphp

    http://www.thinkphp.cn/topic/35958.html <IfModule mod_rewrite.c> Options +FollowSymlinks -Mult ...