[POJ1985] Cow Marathon 「树的直径」】的更多相关文章

>传送门< 题意:求树的直径 思路:就是道模板题,两遍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…
用两次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…
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…
传送门 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…
http://poj.org/problem?id=1985 题意:给出树,求最远距离. 题意: 树的直径. 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点,再从终点进行BFS,则第二次BFS找到的最长路即为树的直径. 原理: 设起点为u,第一次BFS找到的终点v一定是树的直径的一个端点 .证明: 1) 如果u 是直径上的点,则v显然是直径的终点(因为如果v不是的话,则必定存在另一个点w使得u到w的距离更长,则于BFS找到了v矛盾) 2) 如果u不是直径…
题目大意:给你一棵树,要你求树的直径的长度 思路:随便找个点bfs出最长的点,那个点一定是一条直径的起点,再从那个点BFS出最长点即可 以下研究了半天才敢交,1.这题的输入格式遵照poj1984,其实就是把后面的字母无视即可 2.这题数据量没给,所以把数组开得很大才敢交TUT #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <…
选定任意一个点u,从u开始BFS求出距离u最大的点s,再从s点出发BFS到距离s最大的点t,则dis(s,t)即为树的直径 证明 其实只要找到了树的直径的一个端点,再BFS找到最远点就一定是直径的另一个端点,这一点毋庸置疑.所以解法的后半部分一定是正确的,只需要证明第一次BFS找到的最远点k就是其中一个端点s. (一):u在直径上:反证法:若点k不是直径的端点,这意味着dis(u,k)+dis(u,t) < dis(u,s)+dis(u,t),两边同时减去dis(u,t),得dis(u,k) <…
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在直径上,那么能到…
Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4424   Accepted: 2214 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…