POJ 1985 Cow Marathon (模板题)(树的直径)
<题目链接>
题目大意:
给定一颗树,求出树的直径。
解题分析:
树的直径模板题,以下程序分别用树形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 (模板题)(树的直径)的更多相关文章
- 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 (求树的直径)
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 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(树的直径模板)
http://poj.org/problem?id=1985 题意:给出树,求最远距离. 题意: 树的直径. 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点, ...
- 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【树的直径】
题目大意:给你一棵树,要你求树的直径的长度 思路:随便找个点bfs出最长的点,那个点一定是一条直径的起点,再从那个点BFS出最长点即可 以下研究了半天才敢交,1.这题的输入格式遵照poj1984,其实 ...
随机推荐
- A Graph Partitioning Game Theoretical Approach for the VNF Service Chaining Problem
文章名称:A Graph Partitioning Game Theoretical Approach for the VNF Service Chaining Problem 发表时间:2017 期 ...
- CVE-2017-7494 Linux Samba named pipe file Open Vul Lead to DLL Execution
catalogue . 漏洞复现 . 漏洞代码原理分析 . 漏洞利用前提 . 临时缓解 && 修复手段 1. 漏洞复现 . SMB登录上去 . 枚举共享目录,得到共享目录/文件列表,匿 ...
- 金融量化分析【day112】:量化平台的使用-下单函数
order - 按股数下单 1.代码 # 导入函数库 import jqdata #初始化函数,设定基准等等 def initialize(context): set_benchmark('00030 ...
- 针对主机CPU idle性能情况需求脚本编写
[环境介绍] 系统环境:Linux + osw + python 2.7.10 [背景描述] 需求:当系统服务器出现性能告警的时候,需要定位具体的时间点来进行有针对性的去查询产生的问题.OSW提供了很 ...
- python 排序之sort
#coding:utf-8 #求列表的第二大值 list_test =[6,2,4,6,1,2,3,4,5] list_test.sort() print list_test[-2] "&q ...
- 边框回归(bounding-Box regression)
转自:https://blog.csdn.net/zijin0802034/article/details/77685438 为什么要边框回归? 什么是边框回归? 边框回归怎么做的? 边框回归为什么宽 ...
- Java z 404
problem: relative 与absolute 绝对和相对定位 为什么缩放页面里会有离开的情况 为什么a链接里与文字无法对齐 这么多代码为什么没有最好 用最简单的代码去执行一个相应的命令 实现 ...
- python跨网段遍历枚举IP地址(转)
转载链接:https://blog.csdn.net/u013042248/article/details/53165508 0x01 代码思路: 利用二进制遍历: 1.将IP地址分割,每一块转换为8 ...
- 迅为iTOP-4418/6818开发板-驱动-实现GPIO扩展
实现 GPIO 扩展,先弄清楚“复用”的概念,将调用这些 GPIO 的驱动去掉配置,重新编译,加到自己的驱动中,就可以实现扩展的 GPIO 的输入和输出.另外必须要先看文档“迅为iTOP-4418开发 ...
- Java 自定义hashmap和hashtable的key注意哪些问题