树的直径:从随意一点出发,BFS找到最远的距离,然后在从该点出发BFS找到最远的距离

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <deque>
#include <vector>
using namespace std;
const int maxn = 10008;
const int inf = 0x3ffffff;
struct Node {
int w, next,to;
Node (int x = 0, int y = 0,int u = 0 ){
to = x;w = y;next = u;
}
}e[maxn*4];
int d[maxn],head[maxn],tot;
void add(int u,int v,int w){
e[tot] = Node(v,w,head[u]);
head[u] = tot++;
e[tot] = Node(u,w,head[v]);
head[v] = tot++;
}
int BFS(int u) {
memset(d,-1,sizeof(d));
d[u] = 0;
int max_int = 0,id = u;
queue <int> q;
q.push(u);
while(!q.empty()){
u = q.front();q.pop();
if(d[u] > max_int) {
max_int = d[u];
id = u;
} for(int i=head[u];i!=-1;i=e[i].next){
if(d[e[i].to] == -1){
d[e[i].to] = d[u] + e[i].w;
q.push(e[i].to);
}
}
}
// cout << id <<endl;
return id;
}
int main(){
int u,v,w; //freopen("input.txt","r",stdin);
memset(head,-1,sizeof(head));
tot = 0;
while(scanf("%d%d%d",&u,&v,&w) == 3)add(u,v,w);
printf("%d\n",d[BFS(BFS(u))]);
return 0;
}

树的直径 poj 2631的更多相关文章

  1. I - 树的直径 POJ - 1383

    The northern part of the Pyramid contains a very large and complicated labyrinth. The labyrinth is d ...

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

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

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

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

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

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

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

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

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

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

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

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

  8. poj 1985 Cow Marathon 树的直径

    题目链接:http://poj.org/problem?id=1985 After hearing about the epidemic of obesity in the USA, Farmer J ...

  9. POJ 1985 Cow Marathon && POJ 1849 Two(树的直径)

    树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...

随机推荐

  1. KVO奥秘

    序言 在iOS开发中,苹果提供了许多机制给我们进行回调.KVO(key-value-observing)是一种十分有趣的回调机制,在某个对象注册监听者后,在被监听的对象发生改变时,对象会发送一个通知给 ...

  2. 【iOS开发-66】QQ设置界面的案例:利用storyboard开发静态的tableView界面,核心是Static Cells

    (1)效果 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2Vpc3ViYW8=/font/5a6L5L2T/fontsize/400/fill/I0JB ...

  3. 使用Reporting Service订阅对域外用户发邮件

    默认情况下使用Reporting Service对域外邮件发送会失败,一般可能会碰到下面的两个错误: ERROR 1: Subscription Error: "The e-mail add ...

  4. Android--------解决ScrollView中嵌套ExpandableListView显示不全的问题

    ##***Scrollview和ExpandableListView嵌套显示不全*** - 当我们将ExpandableListView嵌套在Scrollview之中时,我们必须要确保我们的不居中Ex ...

  5. SQL Server -查看数据库扩展属性

    1.fn_listextendedproperty 函数可以基于对象类型显示单个数据库对象或数据库中所有对象的扩展属性.例如,可以返回表或表中所有列的扩展属性. A.下面的示例显示了数据库本身设置的所 ...

  6. java基础知识2

    58.线程的基本概念.线程的基本状态以及状态之间的关系线程指在程序执行过程中,能够执行程序代码的一个执行单位,每个程序至少都有一个线程,也就是程序本身.Java中的线程有四种状态分别是:运行.就绪.挂 ...

  7. hdu 1085 Holding Bin-Laden Captive!

    Problem Description We all know that Bin-Laden is a notorious terrorist, and he has disappeared for ...

  8. shell脚本练习(短路练习)

    #!/bin/bash #By Spinestars#2013-11-11#This is a lvsnap of auto-create Help(){ echo "Usage: ---d ...

  9. 树上战争(HDU 2545 并查集求解点到根节点长度)

    树上战争 Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  10. POJ 2488 A Knight's Journey(DFS)

    A Knight's Journey Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 34633Accepted: 11815 De ...