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. IOS8开发之实现App消息推送

    第一部分 Apple Push Notification Service 首先第一步当然是介绍一下苹果的推送机制(APNS)咯(ps:其实每一篇教程都有),先来看一张苹果官方对其推送做出解释的概要图. ...

  2. vs2012 aspx 没有设计视图了?

    vs2012的html设计视图没有了!重新安装一次都不行!现在已经通过简单办法来解决了 其实当你打开 HTML设计器 设置时, “启用 HTML设计器"  这里是打勾的!这时千万不要放弃.先 ...

  3. Linux系统性能测试工具sysbench

    1.CPU性能测试 sysbench --test=cpu --cpu-max-prime= --num-threads= run 2.内存性能测试 sysbench --test=memory -- ...

  4. DATABASE LINK 的查看、创建与删除

    1.查看dblink SELECT OWNER,OBJECT_NAME FROM DBA_OBJECTS WHERE OBJECT_TYPE='DATABASE LINK'; 或者 SELECT * ...

  5. Java-Android 之应用停止错误

    在Android在手机上运行的时候: 经常会出现应用程序停止: 一: 因为触发的方法里面没有传值View 对象,方法报错

  6. 网页版 treeview使用中遇到的问题

    <div class="ScrollBar" id="ItemsTree"></div> var cla = $("#Item ...

  7. 学习java随笔第六篇:数组

    一维数组 创建一维数组并输出 public class OneDimensionalArray { public static void main(String argas[]) { int i[]= ...

  8. jQuery 遍历祖先

    祖先是父.祖父或曾祖父等等. 通过 jQuery,您能够向上遍历 DOM 树,以查找元素的祖先. 向上遍历 DOM 树 这些 jQuery 方法很有用,它们用于向上遍历 DOM 树: parent() ...

  9. QuickSort 递归 分治

    QuickSort 参考<算法导论>,<C程序设计语言> #include<stdio.h> void swap(int v[], int i, int j); v ...

  10. 关于 const 成员函数

    成员函数如果是const意味着什么? 有两个流行概念:物理常量性和逻辑常量性. C++对常量性的定义采用的是物理常量性概念,即const 成员函数不可以更改对象内任何non-static成员变量.例如 ...