树的直径 poj 2631

树的直径:从任意一点出发,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;
}
时间: 2024-10-11 12:02:49

树的直径 poj 2631的相关文章

I - 树的直径 POJ - 1383

The northern part of the Pyramid contains a very large and complicated labyrinth. The labyrinth is divided into square blocks, each of them either filled by rock, or free. There is also a little hook on the floor in the center of every free block. Th

POJ 2631 Roads in the North 树的直径

题目大意:裸的树的直径. 思路:随便用一个点跑BFS,求出这个点到所有点的距离,取距离最长的那个点,再用那个点跑BFS,最远的距离就是这棵树的直径. CODE: #include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 20010 using namespace std; int x,y,z;

poj 1985 Cow Marathon 【树的直径】

题目:poj 1985 Cow Marathon 题意:给出一个树,让你求树的直径. 分析: 树的直径:树上两点之间的最大距离. 我们从任意一点出发,BFS一个最远距离,然后从这个点出发,在BFS一个最远距离,就是树的直径. AC代码: /* POJ:1985 Cow Marathon 2014/10/12/21:18 Yougth*/ #include <cstdio> #include <iostream> #include <algorithm> #include

POJ 1985 Cow Marathon(树的直径)

http://poj.org/problem?id=1985 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u和v两点间有一条距离为cost的边. 然后问你该树上最远的两个点的距离是多少?(即树的直径) 分析: 对于树的直径问题, <<算法导论>>(22 2-7)例题有说明. 详细解法: 首先从树上随意一个点a出发, (BFS)找出到这个点距离最远的点b. 然后在从b点出发(BFS)找到距离b点最远的点c. 那么bc间的距离就是树的直径. 证明: 1.    a

POJ 1849 Two (树形dp 树的直径 两种方法)

Two Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 1232   Accepted: 619 Description The city consists of intersections and streets that connect them. Heavy snow covered the city so the mayor Milan gave to the winter-service a list of st

poj:1985:Cow Marathon(求树的直径)

Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5496   Accepted: 2685 Case Time Limit: 1000MS Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has com

POJ 1849 树的直径 Two

如果一个点开始遍历一棵树再回到原点那么每条边走两次. 现在是两个人从同一点出发,那么最后遍历完以后两人离得越远越好. 最后两人所处位置的路径上的边走了一次,其他边走了两次. 要使总路程最小,两人最后停在直径两端. 所以最终答案就是总权值*2 - 树的直径 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 #include <algor

BZOJ 3363 POJ 1985 Cow Marathon 树的直径

题目大意:给出一棵树.求两点间的最长距离. 思路:裸地树的直径.两次BFS,第一次随便找一个点宽搜.然后用上次宽搜时最远的点在宽搜.得到的最长距离就是树的直径. CODE: #include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 80010 using namespace std; int

POJ 1985 Cow Marathon (树形DP,树的直径)

题意:给定一棵树,然后让你找出它的直径,也就是两点中的最远距离. 析:很明显这是一个树上DP,应该有三种方式,分别是两次DFS,两次BFS,和一次DFS,我只写了后两种. 代码如下: 两次BFS: #include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <queue> using namespace std; const int max