<题目链接>

题目大意:

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

解题分析:
树的直径模板题,以下程序分别用树形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. [python]python3.7中文手册

    https://pythoncaff.com/docs/tutorial/3.7.0

  2. Matlab常用函数集锦

    ndims(A)返回A的维数size(A)返回A各个维的最大元素个数length(A)返回max(size(A))[m,n]=size(A)如果A是二维数组,返回行数和列数nnz(A)返回A中非0元素 ...

  3. vue动态添加对象属性,视图不渲染

    发现数据确实改变了.但是视图没有渲染.原因是赋值的问题,应该这样动态增加属性 vm.$set(vm.template.titleAttachInfoDetail,newKey,newVal) vm 当 ...

  4. Shell脚本之grep

    1. 过滤空行   grep -v ^$

  5. css层叠规则,优先级算法

    前言 层叠样式表CSS最基本的一个特性就是层叠.冲突的声明通过层叠进行排序,由此确定最终的文档表示.而这个过程的核心就是选择器及其相关声明的特殊性.重要性.来源及继承机制.本文将详细介绍CSS层叠 特 ...

  6. [Android] Android RxJava2+Retrofit2+OkHttp3 的使用(一) --基础篇 Retrofit2 的使用

    本文是 Android RxJava2+Retrofit2+OkHttp3 的使用(一) --基础篇 Retrofit2 的使用 本文的目标是用 Retrofit写一个网络请求: 本文以从获取天气预报 ...

  7. WEB内容换行

    word-wrap:break-word 单词间换行 word-break:break-all 单词内也可以换行 white-space属性指定元素内的空白怎样处理 normal 默认.空白会被浏览器 ...

  8. Java入门——多态

    Java引用变量有两个类型分别是 编译时类型:声明的类型 运行时类型:实际赋值的类型 如果这两个类型不一样就会出现多态 举例现有类A class A{ public int field = 5; pu ...

  9. js将时间戳转换为日期类型

    function getLocalTime(nS) {       var date = new Date(nS);    Y = date.getFullYear() + '年';    M = ( ...

  10. ubuntu 16.04 安装 Mask_RCNN 遇到的问题集锦

    源码网页(Mask R-CNN for object detection and instance segmentation on Keras and TensorFlow): https://git ...