POJ1985 Cow Marathon (树的直径)】的更多相关文章

题目链接:http://poj.org/problem?id=1985 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 o…
题目大意:给出一棵树.求两点间的最长距离. 思路:裸地树的直径.两次BFS,第一次随便找一个点宽搜.然后用上次宽搜时最远的点在宽搜.得到的最长距离就是树的直径. CODE: #include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 80010 using namespace std; int…
poj1985 Cow Marathon 树的直径裸题 树的直径的一般求法: 任意一点为起点,dfs/bfs找出与它最远的点$u$ 以$u$为起点,dfs/bfs找出与它最远的点$v$ 则$d(u,v)$是一条直径 下面给出poj1985的code(poj2631自行修改) #include<iostream> #include<cstdio> #include<cstring> #define re register using namespace std; int…
Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 3195   Accepted: 1596 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…
http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次bfs: ①任意从一个点u出发bfs,设其能到的最远点为v ②从v出发重新bfs,设其能到达的最远点为s ③则树的直径就是v->s 证明: 若能证明从任意一个点出发,bfs到的最远点一定在树的直径的端点上,那么第二次bfs就可以证明一定正确了,下面来证明第一次bfs正确性: ①若选择的点u在直径上,那么能到…
>传送门< 题意:求树的直径 思路:就是道模板题,两遍dfs就求出来了 Code #include <cstdio> #include <iostream> #include <algorithm> #include <vector> using namespace std; typedef pair<int, int> P; const int MAX_N = 1000005; vector<P> G[MAX_N]; i…
Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5117   Accepted: 2492 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…
用两次dfs求出树的直径,这两次dfs可以写在一起,当然为了方便理解,这里是分开写的. 1 //两次dfs求树的重心 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 7 const int N=40005,M=40005*2; 8 int n,m,tot,p,ed; 9 int d[N],head[N],to[M],w[M],nxt[M]; 10…
树的直径:(无根)树上最长两点间的最长路径,两次dfs即可,第一次dfs任选一点u,找到距离它最远的点s,再从点s进行一次dfs,找到距离s最远的点t,则s-t之间的路径就是树的直径.证明: <http://www.cnblogs.com/wuyiqi/archive/2012/04/08/2437424.html> poj2631 树的直径裸题 #include<cstdio> #include<iostream> #include<algorithm>…
Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4185   Accepted: 2118 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…