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

 #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. C语言 小球撞击反弹

    计算法(略) #include <stdio.h> #include <stdlib.h> int main() { int x, y, a, resu; scanf(&quo ...

  2. Kibana访问报错

    浏览器访问提示:Kibana server is not ready yet 查看日志如下 {"type":"log","@timestamp&quo ...

  3. WPF 很少人知道的科技

    原文:WPF 很少人知道的科技 本文介绍不那么常见的 WPF 相关的知识. 本文内容 在 C# 代码中创建 DataTemplate 多个数据源合并为一个列表显示 使用附加属性做缓存,避免内存泄漏 使 ...

  4. sqlserver还原差异备份

    因为之前遇到还原差异备份,最开始遇到SQLServer报错:"无法还原日志备份或差异备份,因为没有文件可用于前滚".查阅很多资料后,终于得到解决.收集整理成这篇随笔. 问题原因:出 ...

  5. C# 利用MS的 EntLib的Database类编写的DbHelper

    C# 利用MS的 EntLib的Database类编写的DbHelper,由于MS的EntLib对Oracle.SQL Server和MySql已经封装,所以可以该DbHelper可以适用这三种数据库 ...

  6. Java之路---Day09(继承)

    2019-10-23-22:58:23 目录 1.继承 2.区分成员变量重名的方法 3.区分成员方法重名的方法 4.继承中重写与重载的区别 5.继承中覆盖重写的注意事项 6.继承中覆盖重写的设计原则 ...

  7. JavaScript原型链以及Object,Function之间的关系

    JavaScript里任何东西都是对象,任何一个对象内部都有另一个对象叫__proto__,即原型,它可以包含任何东西让对象继承.当然__proto__本身也是一个对象,它自己也有自己的__proto ...

  8. Excel 97-2003版本内部结构与查看方法

    MS-XLS内部结构:BIFF格式 https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-xls/cd03cb5f-ca0 ...

  9. Gitlab配置webhooks实现自动化部署

    Gitlab 自动化部署 原理介绍 配置gitlab当push动作的时候,访问服务器上的一个链接比如www.shenke.group/hook.php hook.php里面写着一行代码,会让服务器gi ...

  10. jsonpath 一个简单实用的工具

    import jsonpath import json data = "{\"a\": \"11\", \"c\": {\&quo ...