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++交,手动加栈了 别人都是用 ...
随机推荐
- 让System.Drawing.Bitmap可以在linux运行
.net core的bitmap使用的是以下类库,但无法在linux运行 https://github.com/CoreCompat/CoreCompat 在linux运行需要安装runtime.li ...
- js 算法排序总结
1.冒泡排序JavaScript代码实现: function bubbleSort(arr) { var len = arr.length; for (var i = 0; i < len; i ...
- Luogu P1256 显示图像
P1256 显示图像 题目描述 古老的显示屏是由N×M个像素(Pixel)点组成的.一个像素点的位置是根据所在行数和列数决定的.例如P(2,1)表示第2行第1列的像素点.那时候,屏幕只能显示黑与白两种 ...
- Luogu P2298 Mzc和男家丁的游戏
Mzc和男家丁的游戏 题目背景 mzc与djn的第二弹. 题目描述 mzc家很有钱(开玩笑),他家有n个男家丁(做过上一弹的都知道).他把她们召集在了一起,他们决定玩捉迷藏.现在mzc要来寻找他的男家 ...
- CentOS 7.3降低内核版本为7.2
查看当前内核版本: [root@nineep ~]# uname -r 2.3.10.0-514.2.2.el7.x86_64 查看当前发行版本: [root@nineep ~]# cat /etc ...
- 洛谷 P1972 BZOJ 1878 [SDOI2009]HH的项链
题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH 不断地收集新的贝壳,因此,他的项链变得越来越长. ...
- poj 2831 次小生成树模板
/*次小生成树 题意:给你一些路径,现在将一部分路径权值减少后问是否可以替代最小生成树里面的边. 解:次小生成树,即将这条边连上,构成一个环 求出任意两点路径之间的除了这条边的最大值,比较这个最大值& ...
- hdu 4950
#include<stdio.h> int main(){ __int64 h,a,b,k,j=0; while(scanf("%I64d%I64d%I64d%I64d" ...
- Linxu用户管理(转)
说明:用户管理的操作涉及root权限,所以以下实例中应该使用sudo或者root用户进行操作. 背景: Linux系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管 ...
- 2015 年度新增开源软件排名TOP100
本榜单包括 2015 年开源中国新收录的 5977 款开源软件中,依据软件本身的关注度.活跃程度进行排名前 100 名的软件.从这份榜单中也许能够了解到最新业界的趋势. 1.SwitchyOmega ...