Cow Marathon
poj1985:http://poj.org/problem?id=1985
题意:就是树的直径。
题解:直接DFS即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int N=1e5+;
struct Node{
int v;
int w;
};
vector<Node>Q[N];
int n,m,k,u,v;
int maxn,start;
void DFS(int u,int f,int len){
if(len>maxn){
maxn=len;
start=u;
}
for(int i=;i<Q[u].size();i++){
if(f!=Q[u][i].v){
DFS(Q[u][i].v,u,len+Q[u][i].w);
}
}
}
int main(){
while(~scanf("%d%d",&n,&m)){
for(int i=;i<=n;i++)
Q[i].clear();
for(int i=;i<=m;i++){
scanf("%d %d %d",&u,&v,&k);
getchar();getchar();
Node temp;temp.v=v;temp.w=k;
Q[u].push_back(temp);
temp.v=u;temp.w=k;
Q[v].push_back(temp);
}
start=,maxn=;
DFS(,,);
maxn=;
DFS(start,,);
printf("%d\n",maxn); }
}
Cow Marathon的更多相关文章
- poj1985 Cow Marathon (求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 3195 Accepted: 1596 Case ...
- 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 ...
- 树的直径 【bzoj3363】[Usaco2004 Feb]Cow Marathon 奶牛马拉松
3363: [Usaco2004 Feb]Cow Marathon 奶牛马拉松 Description 最近美国过度肥胖非常普遍,农夫约翰为了让他的奶牛多做运动,举办了奶牛马拉松.马拉 松路线要尽 ...
- 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 ...
- POJ 1985 Cow Marathon (求树的直径)
Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...
- POJ P1985 Cow Marathon 题解
这道题是我们考试的第一题,非常水,就是一个树的直径的板子.详见上一篇博客. #include<iostream> #include<cstdio> #include<cs ...
随机推荐
- NDK开发之JNIEnv参数详解
即使我们Java层的函数没有参数,原生方法还是自带了两个参数,其中第一个参数就是JNIEnv. 如下: native方法: public native String stringFromC(); pu ...
- ubuntu首次给root用户设置密码
用过ubuntu的人都知道,刚安装好root用户是没有密码的,没有密码我们就没法用root用户登录 给root用户设置密码输入命令sudo passwd,然后系统会让你输入密码,这时输入的密码就是ro ...
- WSDL阅读方法
我们以天气预报WebService服务为例,来看看怎么阅读一个wsdl文档. 打开一个wsdl文档后,先看底部. binding在这里: portType在这里: 好了,看了上面的,我们来说说wsdl ...
- Ⅱ.AngularJS的点点滴滴--缓存
模板缓存-$templateCache and 缓存工厂 $cacheFactory 1.使用script标签 <html ng-app> <script src="htt ...
- RedHat7 SELinux
SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统.NSA是在Linux社区的帮助下开发了一种 ...
- Chrome浏览器离线下载地址(Stable/Beta/Dev)
最新稳定版:https://www.google.com/intl/zh-CN/chrome/browser/?standalone=1 最新测试版:https://www.google.com/in ...
- linux下实现redis共享session的tomcat集群
为了实现主域名与子域名的下不同的产品间一次登录,到处访问的效果,因此采用rediss实现tomcat的集群效果.基于redis能够异步讲缓存内容固化到磁盘上,从而当服务器意外重启后,仍然能够让sess ...
- 解决Flash挡住层用z-index无效的问题
有时我们要用flash做透明背景结果发现加好之后下面的文字连接点击不了了,div下拉也给flash档住了,后来百度发现我们只要设置wmode参数就可解决了. 在HTML中,如果嵌入Flash,默认 ...
- CI框架篇之控制器篇--设置路由(1)
CodeIgniter 定义默认控制器 当你的网站不存在某个URI 或者 用户直接从根目录访问的时候,CodeIgniter 会加载默认控制器. 打开 application/config/route ...
- UML图基本类型
use case model用例模型 analysiss model分析模型 design model设计模型 implementation model实现模型 deployment model部署模 ...