这道题是我们考试的第一题,非常水,就是一个树的直径的板子。详见上一篇博客。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 40000
using namespace std;
int n,m,cnt=,ans,f_num;
int head[*maxn+],d[maxn+];
struct node
{
int u,v,w,nex;
}edge[*maxn+];
inline void add(int x,int y,int z)
{
cnt++;
edge[cnt].u=x;
edge[cnt].v=y;
edge[cnt].w=z;
edge[cnt].nex=head[x];
head[x]=cnt;
}
inline void dfs(int x,int fa)
{
if(ans<d[x])
{
ans=d[x];
f_num=x;
}
for(int i=head[x];i!=-;i=edge[i].nex)
{
int to=edge[i].v;
if(to==fa)continue;
d[to]=d[x]+edge[i].w;
dfs(to,x);
}
}
int main()
{
freopen("marathon.in","r",stdin);
freopen("marathon.out","w",stdout);
memset(head,-,sizeof(head));
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int x,y,z;
char xb;
scanf("%d%d%d",&x,&y,&z);
cin>>xb;
add(x,y,z);
add(y,x,z);
}
ans=;
d[]=;
dfs(,);
ans=;
d[f_num]=;
dfs(f_num,);
printf("%d",ans);
return ;
}
请各位大佬斧正(反正我不认识斧正是什么意思)

POJ P1985 Cow Marathon 题解的更多相关文章

  1. poj 1985 Cow Marathon

    题目连接 http://poj.org/problem?id=1985 Cow Marathon Description After hearing about the epidemic of obe ...

  2. poj 1985 Cow Marathon【树的直径裸题】

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4185   Accepted: 2118 Case ...

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

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5496   Accepted: 2685 Case ...

  4. 题解报告:poj 1985 Cow Marathon(求树的直径)

    Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...

  5. poj 1985 Cow Marathon 树的直径

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

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

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

  7. POJ 1985 Cow Marathon(树的直径模板)

    http://poj.org/problem?id=1985 题意:给出树,求最远距离. 题意: 树的直径. 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点, ...

  8. POJ 1985 Cow Marathon (求树的直径)

    Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...

  9. POJ 1985 Cow Marathon (模板题)(树的直径)

    <题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...

随机推荐

  1. python学习-33 max和min函数的高级使用

    1.简单比较 age_dic={'age1456':15,'age2':16,'xiaohong_age':12,'xiaoming_age4':18,'age5':10} print(max(age ...

  2. Python解析 算数表达式求值 栈的使用

    使用Python实现一种算数表达式求值的算法,模拟这种使用栈的方式,这是由E.W.Dijkstra在20世纪60年代发明的一种非常简单的算法.代码模拟仅仅表现一种编程思想,代码的逻辑并不完全: if ...

  3. java之spring mvc之拦截器

    1. springmvc 中的拦截器是由实现 HandlerInterceptor 或者继承 HandlerInterceptorAdapter 来实现的. 2. 自定义实现一个拦截器的步骤: a). ...

  4. asp.net core 之中间件

    Http请求资源的过程可以看成一个管道:“Pipe”,并不是所有的请求都是合法的.安全的,其于功能.性能或安全方面的考虑,通常需要在这管道中装配一些处理程序来筛选和加工这些请求.这些处理程序就是中间件 ...

  5. if __name__ == '__main__' 该如何理解

    Python 中的 if __name__ == '__main__' 该如何理解 程序入口 对于很多编程语言来说,程序都必须要有一个入口,比如 C,C++,以及完全面向对象的编程语言 Java,C# ...

  6. jq+swiper 实现今日头条App的选项卡效果

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  7. 关于Python学习之 列表与字典

    列表 列表是Python中最具灵活性的有序集合对象类型. # 列表迭代和解析 >>> res = [c*4 for c in 'Spam'] >>> res ['S ...

  8. unity读取Texture文件并转为Sprit

    using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; usin ...

  9. 修改Nodejs内置的npm默认配置路径方法

    Nodejs 内置的npm默认会把模块安装在c盘的用户AppData目录下(吐槽一下:不明白为啥现在的软件都喜欢把资源装在这里) C盘这么小,肯定是不行的,下面一步步修改到D盘 1.打开cmd命令行, ...

  10. Tomcat启动堆栈异常

    近日部署项目到Tomcat,访问一直404. 于是查看 catalina.out 日志信息,发现如下信息 30-Jun-2019 16:38:03.642 SEVERE [main] org.apac ...