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

 #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. django+uWSGI+nginx的工作原理流程与部署过程

    django+uWSGI+nginx的工作原理流程与部署过程 一.前言 知识的分享,不应该只是展示出来,还应该解释这样做是为什么... 献给和我一样懵懂中不断汲取知识,进步的人们. 授人与鱼,不如授人 ...

  2. Linux基础(05)socket编程

    Linux的核心思想之一 "一切皆文件" 内容 , socket在Linux内核的实现的代码及TCP和UDP的实现 网络编程常用头文件: https://blog.csdn.net ...

  3. hadoop2.x大数据视频教程(十二天学会)

  4. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  5. json_rpc_2 implementation

    https://stackoverflow.com/questions/52670255/flutter-json-rpc-2-implementation import 'dart:convert' ...

  6. js模块基础练习题

    题目描述 完成函数 createModule,调用之后满足如下要求: 1.返回一个对象 2.对象的 greeting 属性值等于 str1, name 属性值等于 str2 3.对象存在一个 sayI ...

  7. Android App 架构演变

    文:https://www.jianshu.com/p/ce26e7960926 最近App项目(MVC架构)越做越大,协同开发效率较低,维护困难,所以产生了调整架构的想法,在 简书.csdn.知乎上 ...

  8. Windows上安装ElasticSearch7的IK分词器

    首先IK分词器和ES版本一定要严格对应,下面是版本对照表 IK分词器下载地址 https://github.com/medcl/elasticsearch-analysis-ik/releases 我 ...

  9. 3.live555源码分析----延时队列

    live555本身是一个单进程.单线程的服务器,但是它能够完美的让多个客户端同时连接,除了使用select并发编程以外,延时队列是很重要的手段. 当连接一个客户端,进行视频帧传输的时候,是不能持续进行 ...

  10. idea安装与注册码破解

    idea安装与注册码破解 https://www.cnblogs.com/jajian/p/7989032.html