poj2631 树的直径】的更多相关文章

设s-t是这棵树的直径,那么对于任意给予的一点,它能够到达的最远的点是s或者t. 这样我们可以通过2次bfs找到树的直径了. #include<cstdio> #include<queue> #include<cstring> #include<iostream> #include<algorithm> using namespace std; ; struct node { int to; int v; int next; }edge[MAXN…
//Accepted 492 KB 0 ms //树的直径 bfs #include <cstdio> #include <cstring> #include <iostream> #include <queue> using namespace std; ; struct node { int u,v,c; node() { } node(int u,int v,int c):u(u),v(v),c(c) { } }p[*imax_n]; int e; i…
题目链接:https://cn.vjudge.net/problem/POJ-2631 树的直径:树中的最长链 具体思路:随便找一个点bfs,然后找到最长的链,然后再以找到的点作为起点进行bfs,然后找到的最长的链就是树的直径. AC代码: #include<iostream> #include<stack> #include<iomanip> #include<queue> #include<iomanip> #include<vecto…
树的直径:(无根)树上最长两点间的最长路径,两次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>…
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…
题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: 假设 s-t这条路径为树的直径,或者称为树上的最长路 现有结论,从任意一点u出发搜到的最远的点一定是s.t中的一点,然后在从这个最远点开始搜,就可以搜到另一个最长路的端点,即用两遍广搜就可以找出树的最长路 证明:   1.设u为s-t路径上的一点,结论显然成立,否则设搜到的最远点为T则   dis…
题目大意:给定一棵 N 个节点的边权无根树,求树的直径. 代码如下 #include <cstdio> #include <algorithm> using namespace std; const int maxn=1e4+10; struct node{ int nxt,to,w; }e[maxn<<1]; int tot=1,head[maxn]; inline void add_edge(int from,int to,int w){ e[++tot].nxt=…
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…
描述 W市的交通规划出现了重大问题,市政府下决心在全市的各大交通路口安排交通疏导员来疏导密集的车流.但由于人员不足,W市市长决定只在最需要安排人员的路口安放人员.具体说来,W市的交通网络十分简单,它包括n个交叉路口和n-1条街道,任意一条街道连接两个交叉路口,并且任意两个交叉路口之间都存在一条路径互相连接.经过长期调查结果显示如果一个交叉路口位于W市交通网的最长路径上,那么这个路口必然拥挤不堪,所谓最长路径定义为某条路径p=(v1,v2,v3…vk),路径经过的路口各不相同且城市中不存在长度>k…
1912: [Apio2010]patrol 巡逻 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 1034  Solved: 562[Submit][Status][Discuss] Description Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示村庄a与b之间有一条道路(1 ≤ a, b ≤ n). Output 输出一个整数,表示新建了K 条道路后能达到的最小巡逻距离.…