POJ P1985 Cow Marathon 题解
这道题是我们考试的第一题,非常水,就是一个树的直径的板子。详见上一篇博客。
#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 题解的更多相关文章
- poj 1985 Cow Marathon
题目连接 http://poj.org/problem?id=1985 Cow Marathon Description After hearing about the epidemic of obe ...
- poj 1985 Cow Marathon【树的直径裸题】
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 4185 Accepted: 2118 Case ...
- poj:1985:Cow Marathon(求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5496 Accepted: 2685 Case ...
- 题解报告:poj 1985 Cow Marathon(求树的直径)
Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...
- poj 1985 Cow Marathon 树的直径
题目链接:http://poj.org/problem?id=1985 After hearing about the epidemic of obesity in the USA, Farmer J ...
- POJ 1985 Cow Marathon && POJ 1849 Two(树的直径)
树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...
- POJ 1985 Cow Marathon(树的直径模板)
http://poj.org/problem?id=1985 题意:给出树,求最远距离. 题意: 树的直径. 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点, ...
- POJ 1985 Cow Marathon (求树的直径)
Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...
- POJ 1985 Cow Marathon (模板题)(树的直径)
<题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...
随机推荐
- day56——http协议、MVC和MTV框架模式、django下载安装、url路由分发
day56 昨日复习 今日内容 HTTP协议 网页:https://www.cnblogs.com/clschao/articles/9230431.html 老师整理的重点 老师整理的重点 请求信息 ...
- Hive学习笔记(三)—— 数据类型
Hive的基本使用(一)--数据类型 1. Hive的基本数据类型 Hive数据类型 Java数据类型 长度 例子 TINYINT byte 1byte有符号整数 20 SMALINT short 2 ...
- 1、Linux安装前的准备
1.硬盘和分区 1.1 Linux中如何表示硬盘和分区 硬盘划分为 主分区.扩展分区和逻辑分区三部分. 主分区只有四个: 扩展分区可以看成是一个特殊的主分区类型,在扩展分区中还可以建立相应的逻辑分区 ...
- 让js中的函数只有一次有效调用
设置隐藏域~ <input type="hidden" value="1" id="flag" /> 其它三种方法
- 16 doc values 【正排索引】
搜索的时候,要依靠倒排索引:排序的时候,需要依靠正排索引,看到每个document的每个field,然后进行排序,所谓的正排索引,其实就是doc values 在建立索引的时候,一方面会建立倒排索引, ...
- tf.nn.softmax_cross_entropy_with_logits()函数的使用方法
import tensorflow as tf labels = [[0.2,0.3,0.5], [0.1,0.6,0.3]]logits = [[2,0.5,1], [0.1,1,3]] a=tf. ...
- pandas-14 concatenate和combine_first的用法
pandas-14 concatenate和combine_first的用法 concatenate主要作用是拼接series和dataframe的数据. combine_first可以做来填充数据. ...
- 【面试突击】-SpringBoot面试题(一)
Spring Boot 是微服务中最好的 Java 框架. 我们建议你能够成为一名 Spring Boot 的专家. 问题一 Spring Boot.Spring MVC 和 Spring 有什么区别 ...
- vue插件(还真是第一次接触)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Oracle数据库之初识部分知识
Oracle是比MySql更为严格的数据库.使用时需要更加严谨. 一.安装注意事项: 1.选择地址时需要注意好不能有汉字,以免造成安装的时候注册表显示监听失败: 2.可视化窗口PLSQL编辑器(相当于 ...