Cow Marathon

poj1985:http://poj.org/problem?id=1985

题意:就是树的直径。

题解:直接DFS即可。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<vector>
 6 using namespace std;
 7 const int N=1e5+10;
 8 struct Node{
 9    int v;
10    int w;
11 };
12 vector<Node>Q[N];
13 int n,m,k,u,v;
14 int maxn,start;
15 void DFS(int u,int f,int len){
16     if(len>maxn){
17         maxn=len;
18         start=u;
19     }
20     for(int i=0;i<Q[u].size();i++){
21         if(f!=Q[u][i].v){
22             DFS(Q[u][i].v,u,len+Q[u][i].w);
23         }
24     }
25 }
26 int main(){
27     while(~scanf("%d%d",&n,&m)){
28          for(int i=1;i<=n;i++)
29              Q[i].clear();
30          for(int i=1;i<=m;i++){
31             scanf("%d %d %d",&u,&v,&k);
32             getchar();getchar();
33             Node temp;temp.v=v;temp.w=k;
34             Q[u].push_back(temp);
35             temp.v=u;temp.w=k;
36             Q[v].push_back(temp);
37          }
38         start=0,maxn=0;
39          DFS(1,0,0);
40          maxn=0;
41          DFS(start,0,0);
42       printf("%d\n",maxn);
43
44     }
45 }

Cow Marathon,布布扣,bubuko.com

时间: 2024-10-10 17:12:53

Cow Marathon的相关文章

poj 1985 Cow Marathon 【树的直径】

题目:poj 1985 Cow Marathon 题意:给出一个树,让你求树的直径. 分析: 树的直径:树上两点之间的最大距离. 我们从任意一点出发,BFS一个最远距离,然后从这个点出发,在BFS一个最远距离,就是树的直径. AC代码: /* POJ:1985 Cow Marathon 2014/10/12/21:18 Yougth*/ #include <cstdio> #include <iostream> #include <algorithm> #include

poj1985 Cow Marathon --- 树的直径

树的直径即树中最长的路径的长度. 用两次dfs,第一次从任意点出发求得一个最远点p, 第二次从p出发求得最远点,这条路径就是最长路,即所求. #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include &l

Cow Marathon(树的直径)

传送门 Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5362   Accepted: 2634 Case Time Limit: 1000MS Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has

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

Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5496   Accepted: 2685 Case Time Limit: 1000MS Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has com

poj 1985 Cow Marathon

题目连接 http://poj.org/problem?id=1985 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 ro

树的直径 【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 ? 一个

B - Cow Marathon DFS+vector存图

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 se

【poj1985】 Cow Marathon

http://poj.org/problem?id=1985 (题目链接) 题意 求树上两点间最长距离.题目背景以及输入描述请见poj1984. Solution 树的直径. 代码 // poj1985 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<cmath> #define L

POJ 1985 Cow Marathon(树的直径)

http://poj.org/problem?id=1985 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u和v两点间有一条距离为cost的边. 然后问你该树上最远的两个点的距离是多少?(即树的直径) 分析: 对于树的直径问题, <<算法导论>>(22 2-7)例题有说明. 详细解法: 首先从树上随意一个点a出发, (BFS)找出到这个点距离最远的点b. 然后在从b点出发(BFS)找到距离b点最远的点c. 那么bc间的距离就是树的直径. 证明: 1.    a