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的更多相关文章

  1. poj1985 Cow Marathon (求树的直径)

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 3195   Accepted: 1596 Case ...

  2. poj 1985 Cow Marathon

    题目连接 http://poj.org/problem?id=1985 Cow Marathon Description After hearing about the epidemic of obe ...

  3. poj 1985 Cow Marathon【树的直径裸题】

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4185   Accepted: 2118 Case ...

  4. 树的直径 【bzoj3363】[Usaco2004 Feb]Cow Marathon 奶牛马拉松

    3363: [Usaco2004 Feb]Cow Marathon 奶牛马拉松 Description ​ 最近美国过度肥胖非常普遍,农夫约翰为了让他的奶牛多做运动,举办了奶牛马拉松.马拉 松路线要尽 ...

  5. poj:1985:Cow Marathon(求树的直径)

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5496   Accepted: 2685 Case ...

  6. Cow Marathon(树的直径)

    传送门 Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5362   Accepted: 2634 ...

  7. poj 1985 Cow Marathon 树的直径

    题目链接:http://poj.org/problem?id=1985 After hearing about the epidemic of obesity in the USA, Farmer J ...

  8. 题解报告:poj 1985 Cow Marathon(求树的直径)

    Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...

  9. POJ 1985 Cow Marathon (求树的直径)

    Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...

  10. POJ P1985 Cow Marathon 题解

    这道题是我们考试的第一题,非常水,就是一个树的直径的板子.详见上一篇博客. #include<iostream> #include<cstdio> #include<cs ...

随机推荐

  1. android开发之使用拼音搜索汉字

    国庆回了趟家,昨天真不想走,离家近的感觉太好.唉,不扯这些,说说今天的正事吧. 上篇博客中介绍了自定义AutoCompleteTextView ,但是用到了一个很蹩脚的技术,就是我们事先把每个汉字的拼 ...

  2. CentOS 6.7下iPython提示“WARNING: Readline services not available or not loaded.”的解决办法

    yum install readline-devel 然后,使用pip或者easy_install安装readline即可 pip install readline

  3. Factory Method 工厂方法模式

    Factory method工厂方法模式是一种实现了“工厂”概念的面向对象设计模式.就像其他创建型模式一样,它也是处理在不指定对象具体类型的情况下创建对象的问题.工厂方法模式的实质是“定义一个创建对象 ...

  4. 服务器端PHP多进程编程

    待更新 版权声明:本文为博主原创文章,未经博主允许不得转载.

  5. AngularJS Filter用法详解【转+实际测试】 格式化货币

    AngularJS内建了一些常用的Filter,我们一一来看一下. currencyFilter(currency): 用途:格式化货币 方法原型: function(amount, currency ...

  6. Java中OutOfMemoryError(内存溢出)的情况及解决办法

    java.lang.OutOfMemoryError: Java heap space // TODO Auto-generated method stub Vector v = new Vector ...

  7. android系统体系结构

    android系统底层是建立在Linux系统之上的,如下图 从上图可以看出android系统有五部分组成 1.APPLICATIONS(应用程序层) 包含一些核心应用程序,电子邮件,日历,地图,浏览器 ...

  8. Windows环境下使用cygwin ndk_r9c编译x264

     一.废话 最近学习,第一步就是编译.我们需要编译FFmpag,x264,fdk_aac,下面是x264,网上说的很多都是几百年前的,我亲测完美可用 还是那句话 我能力有限,但是我希望我写的东西能够让 ...

  9. C# 处理图像三种方法对比

    C#本身自带有一定的图像处理能力,即使在不依赖Emgu CV的情况下,也是有很大的潜质的. 不过,最近在处理大量图片时,发现图片数量较少时,处理本身所带来的延时不会让人敏感,但是数量较大时,程序花费大 ...

  10. cocos2dx入门分析 hello world

    打开新建的"findmistress"项目,可以看到项目文件是由多个代码文件及文件夹组成的,其中 Hello World 的代码文件直接存放于该项目文件夹中.下面我们来详细介绍一下 ...