<题目链接>

题目大意:

给定一颗树,求出树的直径。

解题分析:
树的直径模板题,以下程序分别用树形DP和两次BFS来求解。

树形DP:

#include <cstdio>
#include <algorithm>
using namespace std; const int N = 1e5+;
struct Edge{
int to,val,nxt;
Edge(int _to=,int _val=,int _nxt=):to(_to),val(_val),nxt(_nxt){}
}e[N<<];
int n,m,cnt,ans;
int dp1[N],dp2[N],head[N];
//dp1[u]维护以u为根的子树最长链的长度
//dp2[u]维护以u为根的子树次长链的长度,并且最长链与次长链不重合
inline void add(int u,int v,int w){
e[++cnt]=Edge(v,w,head[u]);
head[u]=cnt;
}
void dfs(int u,int fa){
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to,cost=e[i].val;
if(v==fa)continue;
dfs(v,u);
if(dp1[v]+cost>dp1[u])dp2[u]=dp1[u],dp1[u]=dp1[v]+cost;
else if(dp1[v]+cost>dp2[u])dp2[u]=dp1[v]+cost;
}
ans=max(ans,dp1[u]+dp2[u]);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
int u,v,c;char ch;scanf("%d%d%d %c",&u,&v,&c,&ch);
add(u,v,c);add(v,u,c);
}
dfs(,-);
printf("%d\n",ans);
}

BFS

#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std; const int N = 1e5+;
int n,m,cnt,ans,res; struct Edge{
int to,val,nxt;
Edge(int _to=,int _val=,int _nxt=):to(_to),val(_val),nxt(_nxt){}
}edge[N<<];
int head[N],d[N],vis[N]; inline void add(int u,int v,int w){
edge[++cnt]=Edge(v,w,head[u]);head[u]=cnt;
}
void bfs(int s){
memset(vis,,sizeof(vis));
queue<int>que;
vis[s]=;d[s]=;
que.push(s);
while(!que.empty()){
int u=que.front();
que.pop();
for(int i=head[u]; i; i=edge[i].nxt){
int v=edge[i].to;
if(!vis[v]){
d[v]=d[u]+edge[i].val;
vis[v]=;
que.push(v);
if(d[v]>ans) //更新最远距离
ans=d[v],res=v;
}
}
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
int u,v,w;char ch;scanf("%d%d%d %c",&u,&v,&w,&ch);
add(u,v,w);add(v,u,w);
}
bfs();bfs(res); //两遍BFS,第一遍求出直径上的一个端点,第二遍求出另一个端点
printf("%d\n",ans);
}

POJ 1985 Cow Marathon (模板题)(树的直径)的更多相关文章

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

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

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

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

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

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

  4. poj 1985 Cow Marathon

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

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

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

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

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

  7. poj 1985 Cow Marathon 树的直径

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

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

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

  9. POJ 1985 Cow Marathon【树的直径】

    题目大意:给你一棵树,要你求树的直径的长度 思路:随便找个点bfs出最长的点,那个点一定是一条直径的起点,再从那个点BFS出最长点即可 以下研究了半天才敢交,1.这题的输入格式遵照poj1984,其实 ...

随机推荐

  1. LOJ #2533. 「CTSC2018」暴力写挂(边分治合并)

    题意 给你两个有 \(n\) 个点的树 \(T, T'\) ,求一对点对 \((x, y)\) 使得 \[ depth(x) + depth(y) - (depth(LCA(x , y)) + dep ...

  2. [WC2007]剪刀石头布(最大流)

    洛古 一句话题意:给定一张图,每两点之间有一条有向边或无向边,把所有无向边定向,使图中三元环个数尽量多 因为原图是一个完全图,假设图中任意三点都能构成三元环,那么途中三元环的个数为:\(\binom{ ...

  3. zsh fg: no job control in this shell.

    图片的上面就是将一个应用按Ctrl+Z,把任务放到后台里面.没法fg将任务回到前台运行. 在.zshrc中添加set -m. 具体原因不明.我切换到root用户里,没有出现这个问题.将我的.zshrc ...

  4. java替换ascii表字符

    如下: //处理特殊字符 public String dealSpecialXml(String xml){ String result = ""; //result = xml. ...

  5. 使用Redisson实现分布式锁,Spring AOP简化之

    源码 Redisson概述 Redisson是一个在Redis的基础上实现的Java驻内存数据网格(In-Memory Data Grid).它不仅提供了一系列的分布式的Java常用对象,还提供了许多 ...

  6. ubuntu文件搜索统计

    一.在ubuntu下如何搜索文件 1.特点:快速,但是是模糊查找,例如 找 #whereis mysql 它会把mysql,mysql.ini,mysql.*所在的目录都找出来.我一般的查找都用这条命 ...

  7. 第十五节:Expression表达式目录树(与委托的区别、自行拼接、总结几类实例间的拷贝)

    一. 基本介绍 回忆: 最早接触到表达式目录树(Expression)可能要追溯到几年前使用EF早期的时候,发现where方法里的参数是Expression<Func<T,bool> ...

  8. 运维工作笔记——基于centos7.3的多台服务期时间同步

    1. 确认服务器版本 2.查看本机时间 3.可以同过date进行时间更改 4.yum安装ntp服务(服务端与客户端都需要安装)       yum install -y ntp 5.在服务端192.1 ...

  9. pyqt5-按钮基类-QAbstractButton

    QAbstractButton  是抽象类 from PyQt5.QtWidgets import QApplication, QWidget,QAbstractButton import sys f ...

  10. Rich feature hierarchies for accurate object detection and semantic segmentation(理解)

    0 - 背景 该论文是2014年CVPR的经典论文,其提出的模型称为R-CNN(Regions with Convolutional Neural Network Features),曾经是物体检测领 ...