poj1985 / poj2631(树的直径)
树的直径裸题
树的直径的一般求法:
任意一点为起点,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 max(int &a,int &b){return a>b?a:b;}
#define N 50002
int n,m,k,ans,dis[N];
int cnt,hd[N],nxt[N<<],ed[N],poi[N<<],val[N<<];
void adde(int x,int y,int v){
nxt[ed[x]]=++cnt; hd[x]=hd[x]?hd[x]:cnt;
ed[x]=cnt; poi[cnt]=y; val[cnt]=v;
}
void dfs(int x,int fa){
if(dis[x]>dis[k]) k=x;
for(int i=hd[x];i;i=nxt[i]){
int to=poi[i];
if(to==fa) continue;
dis[to]=dis[x]+val[i];
dfs(to,x);
}
}
int main(){
char c[]; int q1,q2,q3;
while(~scanf("%d%d",&n,&m)){
memset(hd,,sizeof(hd));
memset(ed,,sizeof(ed));
memset(nxt,,sizeof(nxt));
ans=cnt=k=;
for(re int i=;i<n;++i){
scanf("%d%d%d%s",&q1,&q2,&q3,c);
adde(q1,q2,q3); adde(q2,q1,q3);
}
dis[]=; dfs(,);
dis[k]=; dfs(k,);
for(re int i=;i<=n;++i) ans=max(ans,dis[i]);
printf("%d\n",ans);
}return ;
}
poj1985 / poj2631(树的直径)的更多相关文章
- poj2631 树的直径
设s-t是这棵树的直径,那么对于任意给予的一点,它能够到达的最远的点是s或者t. 这样我们可以通过2次bfs找到树的直径了. #include<cstdio> #include<qu ...
- poj2631 树的直径 + bfs
//Accepted 492 KB 0 ms //树的直径 bfs #include <cstdio> #include <cstring> #include <iost ...
- POJ 树的直径和重心
树的直径:(无根)树上最长两点间的最长路径,两次dfs即可,第一次dfs任选一点u,找到距离它最远的点s,再从点s进行一次dfs,找到距离s最远的点t,则s-t之间的路径就是树的直径.证明: < ...
- 树的直径(两个bfs)
题目链接:https://cn.vjudge.net/problem/POJ-2631 树的直径:树中的最长链 具体思路:随便找一个点bfs,然后找到最长的链,然后再以找到的点作为起点进行bfs,然后 ...
- WOJ1024 (POJ1985+POJ2631) Exploration 树/BFS
title: WOJ1024 (POJ1985+POJ2631) Exploration 树/BFS date: 2020-03-20 10:43:00 categories: acm tags: [ ...
- poj1985和poj1849(树的直径)
题目传送门:poj1985 树是连通无环图,树上任意两点之间的路径是唯一的.定义树上任 意两点u, v的距离为u到v路径上边权的和.树的直径MN为树上最长路 径,即点M和N是树上距离最远的两个点. 题 ...
- poj2631 求树的直径裸题
题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...
- poj1985 Cow Marathon (求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 3195 Accepted: 1596 Case ...
- [USACO2004][poj1985]Cow Marathon(2次bfs求树的直径)
http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次bfs: ...
随机推荐
- Android 监听按钮的点击事件
onClick事件1.Button和ImageButton都拥有一个onClick事件 通过自身的.setOnClickListener(OnClickListener)方法添加点击事件2.所有的控件 ...
- 当JS出现的Cannot read property 'XXX' of null错误
由于在加载JS的时候,页面还未加载完成,就出现了这样的错误.解决方法很简单,将这段 js 放到页面的最下面,等到所以页面加载完成时,再加载这段JS.
- Jquery跨域Ajax取值
HTML: $.ajax({ type: "get", async: false, url: "http://www.xxxxxx.com/otherLogin/chec ...
- Open vSwitch 工作原理
概念 Open vSwitch中许多网络上的概念与平时接触到的不同,这里介绍一下Open vSwitch中用到的一些名词及概念. Packet (数据包) 网络转发的最小数据单元,每个包都来自某个端口 ...
- bzoj 3307 雨天的尾巴
题目链接:传送门 题目大意:中文题,略 题目思路:网上有题解说是合并线段树的,但是太难蒟蒻不会,只能用树剖求解 如果不是树而是一维数组我们会怎么解? 当然是利用前缀和思想标记 (L) v+1,(R+1 ...
- 【BZOJ3943】[Usaco2015 Feb]SuperBull 最大生成树
[BZOJ3943][Usaco2015 Feb]SuperBull Description Bessie and her friends are playing hoofball in the an ...
- OC开发_Storyboard——block和动画
一.协议 @optional :可选的 @requied :必须实现的 二.block 代码块 1. 以一个^开头,然后是参数,然后是一个大括号,包含我们的代码块 [aDictionary enu ...
- 微信小程序 --- 获取当前坐标
获取位置:get.location type:wgs84(是全球定位系统,获取的坐标,gcj02是国家测绘局给出的坐标) btnclick:function(){ wx.getLocation({ t ...
- js判断浏览器是否安装Flash插件,并提示安装或开启
var flashChecker = function() { var hasFlash = 0; //是否安装了flash var flashVersion = 0; //flash版本 if(do ...
- 使用angular路由切换后 轮播以及iscrollJs失效的问题
我们在使用angular的时候,路由总是最让人头疼的地方. 在这里为大家解决一些用angular来回切换遗留下的小问题 比如我们在使用ng-route时如果主页面含有轮播图,当你切换到其他页面再切回主 ...