树的直径 【bzoj3363】[Usaco2004 Feb]Cow Marathon 奶牛马拉松
3363: [Usaco2004 Feb]Cow Marathon 奶牛马拉松
Description
最近美国过度肥胖非常普遍,农夫约翰为了让他的奶牛多做运动,举办了奶牛马拉松.马拉
松路线要尽量长,所以,告诉你农场的地图(该地图的描述与上题一致),请帮助约翰寻找两个
最远农场间的距离.
Input
第1行:两个分开的整数N和M.
第2到M+1行:每行包括4个分开的内容,Fi,F2,L,D分别描述两个农场的编号,道路的长
度,F1到F2的方向N,E,S,W.
Output
一个整数,表示最远两个衣场间的距离.
找树的直径,水。
m和opt都是没有用的。
code
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int wx=500017;
inline int read(){
int sum=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){sum=(sum<<1)+(sum<<3)+ch-'0';ch=getchar();}
return sum*f;
}
struct e{
int nxt,to,dis;
}edge[wx];
int n,m,k,num;
int ans,maxn,pos;
int head[wx],dis[wx];
void add(int from,int to,int dis){
edge[++num].nxt=head[from];
edge[num].to=to;
edge[num].dis=dis;
head[from]=num;
}
void dfs(int u,int fa){
for(int i=head[u];i;i=edge[i].nxt){
int v=edge[i].to;
if(v==fa)continue;
dis[v]=dis[u]+edge[i].dis;
dfs(v,u);
}
}
int main(){
n=read();read();
for(int i=1;i<n;i++){
int x,y,z;
x=read();y=read();z=read();scanf("%s");
add(x,y,z);add(y,x,z);
}
dfs(1,0);
for(int i=1;i<=n;i++){
if(maxn<dis[i]){
maxn=dis[i];pos=i;
}
}
memset(dis,0,sizeof dis);
dfs(pos,0);
for(int i=1;i<=n;i++){
ans=max(ans,dis[i]);
}
printf("%d\n",ans);
return 0;
}
树的直径 【bzoj3363】[Usaco2004 Feb]Cow Marathon 奶牛马拉松的更多相关文章
- 树的直径【bzoj3363】 [Usaco2004 Feb]Cow Marathon 奶牛马拉松
Description 最近美国过度肥胖非常普遍,农夫约翰为了让他的奶牛多做运动,举办了奶牛马拉松.马拉松路线要尽量长,所以,告诉你农场的地图(该地图的描述与上题一致),请帮助约翰寻找两个最远农场间的 ...
- BZOJ 3363: [Usaco2004 Feb]Cow Marathon 奶牛马拉松
Description 给你一个图,两个点至多有一条路径,求最长的一条路径. \(n \leqslant 4\times 10^4\) Sol DFS?DP? 这就是一棵树,方向什么的都没用... 然 ...
- [USACO2004][poj1985]Cow Marathon(2次bfs求树的直径)
http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次bfs: ...
- poj1985 Cow Marathon (求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 3195 Accepted: 1596 Case ...
- poj 1985 Cow Marathon【树的直径裸题】
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 4185 Accepted: 2118 Case ...
- poj:1985:Cow Marathon(求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5496 Accepted: 2685 Case ...
- Cow Marathon(树的直径)
传送门 Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5362 Accepted: 2634 ...
- 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(求树的直径)
Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...
随机推荐
- PHP使用http_build_query()构造URL字符串的方法
http_build_query http_build_query -- 生成 url-encoded 之后的请求字符串描述string http_build_query ( array formda ...
- [转]sscanf函数具体用法
大学生程序代写 sscanf 名称: sscanf() - 从一个字符串中读进与指定格式相符的数据. 函数原型: Int sscanf( string str, string fmt, mixed v ...
- Maven(5)-优化和重构POM
本文主要介绍如何优化pom,杜绝重复(DRY). 1)模块重复依赖: 2)坐标版本号重复: 3)兄弟依赖 一.项目骨架 上图说明: multi-module-project是一个有多个模块构成的项目, ...
- Impala的JDBC无法连接
这是因为客户端连接的JDBC是Impala的master机器,而不是DataNode:因为JDBC的服务宿主是Impalad,而Impalad只是部署在DataNode
- netty中的ByteBuf
网络数据的基本单位总是字节.Java NIO 提供了 ByteBuffer 作为它 的字节容器,但是这个类使用起来过于复杂,而且也有些繁琐. Netty 的 ByteBuffer 替代品是 ByteB ...
- C语言学习笔记--C语言中变量的属性关键字
变量属性关键字的使用语法:property type var_name; 1.auto 关键字 auto关键字是C语言中局部变量的默认的关键字,C编译器默认所有的局部变量都是auto的,它表明了被修饰 ...
- 如何更新node和npm版本
更新npm ---> npm undate -g 更新node ---> npm install -g -n n latest
- 实现hadoop中的机架感知
hadoop中声明是有机架感知的功能,能够提高hadoop的性能.平时我们使用的hadoop集群,实际上是从来没有使用上这个功能的. hadoop中所说的 机架感知的实现实际上这样的: hadoop启 ...
- supervisor启动worker源码分析-worker.clj
supervisor通过调用sync-processes函数来启动worker,关于sync-processes函数的详细分析请参见"storm启动supervisor源码分析-superv ...
- 6.6 Ubuntu 安装 截图工具 Shutter
可参考: http://blog.csdn.net/hanshileiai/article/details/46843713