POJ 1985 求树的直径 两边搜OR DP
Cow Marathon
Description
After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has committed to create a bovine marathon for his cows to run. The marathon route will include a pair of farms and a path comprised of a sequence of roads between them. Since FJ wants the cows to get as much exercise as possible he wants to find the two farms on his map that are the farthest apart from each other (distance being measured in terms of total length of road on the path between the two farms). Help him determine the distances between this farthest pair of farms.
Input
Lines 1…..: Same input format as “Navigation Nightmare”.
OutputLine 1: An integer giving the distance between the farthest pair of farms.
Sample Input
7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
Sample Output
52
Hint
The longest marathon runs from farm 2 via roads 4, 1, 6 and 3 to farm 5 and is of length 20+3+13+9+7=52.
题意:给你一棵树,求树上的最长路(也就是直径)。
思路:一开始看见输入后面还有方向,顿时懵了。想了想,给了方向,但并没有路也是连不上的。剩下的两次BFS就OK了。(题目的数据范围在前一道题上),数组一定要开到数据范围的2倍!!(有向边,一开始没有注意到。G++无限RE,C++无限WA)
奉上代码。
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
int tot=1,n,m,next[80005],first[40005],w[80005],vis[80005],v[80005],maxx,k;
void BFS()
{
queue<int> q;
memset(vis,0,sizeof(vis));maxx=0;
q.push(k);
while(!q.empty())
{
int t=q.front();q.pop();
for(int i=first[t];i;i=next[i])
if(!vis[v[i]]&&v[i]!=k)
vis[v[i]]=vis[t]+w[i],q.push(v[i]);
}
for(int i=1;i<=n;i++)
if(vis[i]>maxx)
maxx=vis[i],k=i;
}
int main()
{
int x;char s;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d %c",&x,&v[tot],&w[tot],&s);
next[tot]=first[x];first[x]=tot++;w[tot]=w[tot-1];
v[tot]=x;next[tot]=first[v[tot-1]];first[v[tot-1]]=tot++;
}
k=1,BFS(),BFS();
printf("%d",maxx);
}
2016.10.23补充:
这里有一种DP做法
维护一个子树向下的最长链和次长链 (路径没有交)
f[x]表示最长链 g[x]表示次长链 ans=max(ans,f[x]+g[x]);
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
int tot=1,n,m,next[80005],first[40005],w[80005],vis[80005],v[80005],ans;
int f[80005],g[80005];
void dfs(int x,int fa){
for(int i=first[x];i;i=next[i]){
if(v[i]==fa)continue;
dfs(v[i],x);
if(f[v[i]]+w[i]>f[x])g[x]=f[x],f[x]=f[v[i]]+w[i];
else if(f[v[i]]+w[i]>g[x])g[x]=f[v[i]]+w[i];
}
ans=max(ans,f[x]+g[x]);
}
int main(){
int x;char s;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d%d%d %c",&x,&v[tot],&w[tot],&s);
next[tot]=first[x];first[x]=tot++;w[tot]=w[tot-1];
v[tot]=x;next[tot]=first[v[tot-1]];first[v[tot-1]]=tot++;
}
dfs(1,-1);
printf("%d",ans);
}
POJ 1985 求树的直径 两边搜OR DP的更多相关文章
- 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-树的直径-树的直径模板(BFS、DFS(vector存图)、DFS(前向星存图))
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 7536 Accepted: 3559 Case ...
- poj2631 求树的直径裸题
题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...
- [USACO2004][poj1985]Cow Marathon(2次bfs求树的直径)
http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次bfs: ...
- URAL 1145—— Rope in the Labyrinth——————【求树的直径】
Rope in the Labyrinth Time Limit:500MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64 ...
- poj1985 Cow Marathon (求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 3195 Accepted: 1596 Case ...
- hdu 4607 Park Visit 求树的直径
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n) ...
- 4612 warm up tarjan+bfs求树的直径(重边的强连通通分量)忘了写了,今天总结想起来了。
问加一条边,最少可以剩下几个桥. 先双连通分量缩点,形成一颗树,然后求树的直径,就是减少的桥. 本题要处理重边的情况. 如果本来就两条重边,不能算是桥. 还会爆栈,只能C++交,手动加栈了 别人都是用 ...
随机推荐
- 零基础转行Linux云计算运维工程师获得20万年薪的超级学习技巧
云计算概念一旦产生便一发不可收拾,成为移动互联网时代最为火热的行业之一.国内各大互联网公司例如阿里.腾讯.百度.网易等纷纷推出自己的云计算产品,3月10日,腾讯云0.01元投标时间更是让云计算在普罗大 ...
- iptables详解(2):iptables实际操作之规则查询
所属分类:IPtables Linux基础 在阅读这篇文章之前,请确保你已经阅读了如下文章,如下文章总结了iptables的相关概念,是阅读这篇文章的基础. 图文并茂理解iptables 如果你是一 ...
- Eclipse调试DEBUG时快速查看某个变量的值的快捷键
ctrl+shift+i
- 笔记,js对象浅析
学习笔记, 来源:http://www.cnblogs.com/zuiyirenjian/p/3535126.html 作者:醉意人间 此外,关于自运行函数可参考 http://benalman. ...
- ACM蓝桥杯之换硬币问题
题目描述: 想兑换100元零钱,有1元.2元.5元.10元四种面值,总有多少种兑换方法? 解题思路: 本题可以采用多种方法求解.最容易理解的应该就是暴力穷举和递归求解.那么本文主要介绍这两种解法. 暴 ...
- zabbix4.0搭建(基于CentOS6.8)
环境 服务端:188.188.3.241,系统:centos6.8,mysql:5.7.3,php:5.4.9,nginx:1.12.0 一.nginx编译安装 NGINX_VERSION=1.1 ...
- 00.pip安装包
pip安装更换镜像源 pip install 包名 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com pip导出和导入 ...
- 10.多shard场景下relevence score可能不准确
主要知识点 多shard场景下relevence score可能不准确的原因 多shard场景下relevence score可能不准确解决方式 一.多shard场景下relevance sc ...
- 仿探探卡片滑动vue封装并发布到npm
项目初始化使用 webpack-simple 方式比较方便和容易配置,原来的方式各种坑慎入 vue init webpack-simple vue-card-slide cd vue-card-sli ...
- [加强版] Codeforces 835D Palindromic characteristics (回文自动机、DP)
题目链接: https://codeforces.com/contest/835/problem/D 题意: 一个回文串是\(1\)-回文的,如果一个回文串的左半部分和右半部分一样且都是\(k\)-回 ...