POJ1985 Cow Marathon 树的直径

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

求一棵树的直径。

方法:

随便选一个点当做root,开始DFS找到最远的一个点,这个点一定是树直径中的一个端点(严格树,不存在环)

然后再把刚才找到的点作为ROOT 第二遍DFS,这次找到的最远的距离,就是树的直径。

 

Cow Marathon
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 2963 Accepted: 1484
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 committed to create a bovine marathon for his cows to run. The marathon route will include a pair of farms and a path comprised of a sequence of roads between them. Since FJ wants the cows to get as much exercise as possible he wants to find the two farms on his map that are the farthest apart from each other (distance being measured in terms of total length of road on the path between the two farms). Help him determine the distances between this farthest pair of farms.

Input

* Lines 1.....: Same input format as "Navigation Nightmare".

Output

* Line 1: An integer giving the distance between the farthest pair of farms.

Sample Input

7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S

Sample Output

52

 

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>

using namespace std;

#define MAXN  500000

struct edge
{
    int to,dis,next;
}e[9999999];

int head[MAXN],en;
int dis[MAXN];

void add(int u,int v ,int w)
{
    e[en].to=v;
    e[en].dis=w;
    e[en].next=head[u];
    head[u]=en++;
}

void dfs(int f,int u,int d)
{
    dis[u]=d;
    for(int i=head[u];i!=-1;i=e[i].next)
        if(e[i].to!=f)
            dfs(u,e[i].to,d+e[i].dis);
}

int u,v,c;
int n,m;
char s[20];
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(head,-1,sizeof(head));
        en=0;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d%s",&u,&v,&c,s);
            add(u,v,c);
            add(v,u,c);
        }
        dfs(0,1,0);
        int maxs,tag;
        maxs=-1;tag=0;
        for(int i=1;i<=n;i++)
        {
            if(maxs<dis[i])
            {
                maxs=dis[i];
                tag=i;
            }
        }
        dfs(0,tag,0);
        maxs=-1;
        for(int i=1;i<=n;i++)
            maxs=max(maxs,dis[i]);
        printf("%d\n",maxs);
    }
    return 0;
}

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

相关推荐

[POJ 1985][直径]Cow Marathon

题目描述: 题目链接: POJ 1985 Cow Marathon After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has committed to create a bovine marathon for his cows

千古的博客 1439

poj1985 Cow Marathon直径

Cow Marathon Time Limit:2000MS Memory Limit:30000K Total Submissions:9653 Accepted:4411 Case Time Limit:1000MS Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, ...

530

[学习][poj1985]直径 Cow Marathon

直径的定义 一棵上的最长路径。直径的实现 在中随便找一个点进行dfs,再对找出的距离此点最远的点进行dfs,此时的最远距离就是直径。 证明如下: 如图,若s——>t是直径,我们在中随便找一点u,并找距此点最远的点v,若v不是s也不是t,说明u到v的距离大于u到s和u到t的距离,即s到u再到v的距离大于s到t的距离,故直径不是s——>t,矛盾。 即u能找到的最远点一

g19zwk的博客 468

[POJ 1985] Cow Marathon (直径

题目链接:

SIOFive的专栏 1266

(直径)POJ1985Cow Marathon

POJ1985Cow Marathon 题意: 给一个森林,求直径的最大值。 思路: 求直径的模板题。两次dfs(bfs)即可。 这篇博客讲的比较好。 代码: #include<iostream> #include<stdio.h> #include<stdlib.h> #include<algorithm> #include<string.h> #include<ctype.h> #include<queue> #

Z7784562的博客 276

POJ 1985 Cow Marathon 直径

求一棵上的最长路,即直径。 #include #include #include #include #include #include #include #include #include #include #include #include #define PI acos(-1.0) #define Max 20005 #define inf 1<<28 #def

just_water 1035

直径学习小记 Poj 1985 Cow Marathon+Poj 2631 Roads in the North

1. 上面求最长路简单路(无环). 就是直径问题. 2. 直径问题经典解法:两遍BFS                          (1). 一开始任取一个点u进行搜索查找出距离点u最远距离的点v和长度.                          (2). 第二次BFS则从第一次中的点v找出距离点v最远距离的点的路径长度.                3. 问题正

whyorwhnt的专栏 1161

POJ1985 Cow Marathon直径

http://poj.org/problem?id=1985 Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has committed to create a bovine marathon fo...

lanshan1111的博客 241

POJ 1985 Cow Marathon直径

POJ 1985 Cow Marathon 总时间限制:  2000ms 单个测试点时间限制:  1000ms   内存限制:  65536kB 描述 After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has ...

不甘 253

Cow Marathon POJ - 1985 直径

题意: 给出n,m,然后m条边,求直径。 思路:两遍dfs。 代码: #include<iostream> #include<algorithm> #define rep(i,a,b) for(int i=a;i<=b;i++) #define ll long long #include<string.h> #include<queue...

lanshan1111的博客 223

【数据结构】POJ 1985 Cow Marathon直径

链接: http://poj.org/problem?id=1985 题意: 有 nnn 个农场和 mmm 条路,以及每条路的方向(方向在这道题中没有用)。并且每对农场之间只有一条路可达,即这是一个。求最长的一条路,也就是两点间的最大距离,即直径。 思路: 直径的最长简单路(中所有路径的最大值)。 求解方法: 跑两遍 BFS。第一遍 BFS 先任选一个起点,BFS 找到最长路的终...

Krone_的博客 332

poj1985 Cow Marathon直径#入门)

poj1985 Cow Marathon直径) Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 9110 Accepted: 4171 Case Time Limit: 1000MS Description After hearing about the epidemic of obesity in the USA...

tb_youth的博客 321

POJ 1985 Cow Marathon直径

直径 给定一棵中每条边都有一个权值,中两点之间的距离定义为连接两点的路径边权之和。中最远的两个节点之间的距离被称为直径,连接这两点的路径被称为的最长链。后者通常也可称为直径,即直径是一个数值概念,也可代指一条路径。 求法 先从任意一点P出发,找离它最远的点Q,再从点Q出发,找离它最远的点W,W到Q的距离就是是的直径。可以用两次dfs先搜到1号点距离最长的点p,然后在搜到p距离最...

H4ppyD0g的博客 383

POJ1985 - Cow Marathon直径

点击打开题目 Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4846   Accepted: 2371 Case Time Limit: 1000MS Description After hearing abo

wyg1997 1014

直径 Cow Marathon poj-1985

这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入 欢迎使用Ma...

Kk的博客 205

POJ 1985--Cow Marathon直径 && 模板】

Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4182   Accepted: 2116 Case Time Limit: 1000MS Description After hearing about the epidemi

_阿阿阿阿欢 967

POJ Problem 1985 Cow Marathon直径

Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4882   Accepted: 2380 Case Time Limit: 1000MS Description After hearing about the epidemic

博客已搬迁到http://www.cnblogs.com/cniwoq/ 529

POJ 1985 Cow Marathon(直径)

  题目链接:http://poj.org/problem?id=1985        题意是给了n个点m条边,求任意两点间的最大值。        这道题的题意感觉描述的不清楚,而且还没有数据范围...就是一道裸的求直径的题,输入的那个NSWD没用... AC代码: #include &lt;iostream&gt; #include &lt;cstdio&gt; #inclu...

Charles_Zaqdt的博客 228
上一篇: POJ1465 Multiple BFS+同余
下一篇: HDU1026 Ignatius and the Princess I BFS+路径输出
Albafica
博客等级 码龄15年 98粉丝 359原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值