poj1985 / 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…
树的直径:(无根)树上最长两点间的最长路径,两次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>…
题目链接:https://cn.vjudge.net/problem/POJ-2631 树的直径:树中的最长链 具体思路:随便找一个点bfs,然后找到最长的链,然后再以找到的点作为起点进行bfs,然后找到的最长的链就是树的直径. AC代码: #include<iostream> #include<stack> #include<iomanip> #include<queue> #include<iomanip> #include<vecto…
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…
title: WOJ1024 (POJ1985+POJ2631) Exploration 树/BFS date: 2020-03-20 10:43:00 categories: acm tags: [acm,woj,图论] 用到了树的 直径 1 描述 Tom is an explorer and now he is in a mysterious cave. He finds that there are lots of rooms in it. You are ensured that the…
题目传送门:poj1985 树是连通无环图,树上任意两点之间的路径是唯一的.定义树上任 意两点u, v的距离为u到v路径上边权的和.树的直径MN为树上最长路 径,即点M和N是树上距离最远的两个点. 题目就是寻找树的直径的版子题,两次dfs(第一次遍历根节点所到达的最远距离x点,第二次dfs从x到达最远距离y,这个就是树的直径),或者树形dp,我这里用到了树形dp: 还要感谢石神对我的教导和更正,让我接触了树形dp: 但这个题的代码就不给出了,因为在下面一题的代码中会体现.. 题目:poj1849…
题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: 假设 s-t这条路径为树的直径,或者称为树上的最长路 现有结论,从任意一点u出发搜到的最远的点一定是s.t中的一点,然后在从这个最远点开始搜,就可以搜到另一个最长路的端点,即用两遍广搜就可以找出树的最长路 证明:   1.设u为s-t路径上的一点,结论显然成立,否则设搜到的最远点为T则   dis…
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在直径上,那么能到…