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 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(树的直径)的更多相关文章

  1. poj2631 树的直径

    设s-t是这棵树的直径,那么对于任意给予的一点,它能够到达的最远的点是s或者t. 这样我们可以通过2次bfs找到树的直径了. #include<cstdio> #include<qu ...

  2. poj2631 树的直径 + bfs

    //Accepted 492 KB 0 ms //树的直径 bfs #include <cstdio> #include <cstring> #include <iost ...

  3. POJ 树的直径和重心

    树的直径:(无根)树上最长两点间的最长路径,两次dfs即可,第一次dfs任选一点u,找到距离它最远的点s,再从点s进行一次dfs,找到距离s最远的点t,则s-t之间的路径就是树的直径.证明: < ...

  4. 树的直径(两个bfs)

    题目链接:https://cn.vjudge.net/problem/POJ-2631 树的直径:树中的最长链 具体思路:随便找一个点bfs,然后找到最长的链,然后再以找到的点作为起点进行bfs,然后 ...

  5. WOJ1024 (POJ1985+POJ2631) Exploration 树/BFS

    title: WOJ1024 (POJ1985+POJ2631) Exploration 树/BFS date: 2020-03-20 10:43:00 categories: acm tags: [ ...

  6. poj1985和poj1849(树的直径)

    题目传送门:poj1985 树是连通无环图,树上任意两点之间的路径是唯一的.定义树上任 意两点u, v的距离为u到v路径上边权的和.树的直径MN为树上最长路 径,即点M和N是树上距离最远的两个点. 题 ...

  7. poj2631 求树的直径裸题

    题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...

  8. poj1985 Cow Marathon (求树的直径)

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 3195   Accepted: 1596 Case ...

  9. [USACO2004][poj1985]Cow Marathon(2次bfs求树的直径)

    http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次bfs: ...

随机推荐

  1. 解决Xcode "The selected destination does not support the architecture " 错误错误

    XCODE编译运行项目后,发现工程编译后无法运行,出现:"The selected destination does not support the architecture for whi ...

  2. 谈一谈php://input和php://output

    对一php://input介绍,PHP官方手册文档有一段话对它进行了很明确地概述. php://input 是个可以访问请求的原始数据的只读流. POST 请求的情况下,最好使用 php://inpu ...

  3. Ubuntu 12.04 部署 PostGIS 2.1

    首先,卸载掉原有的postgis和postgresql-9.1-postgis,不然你就用1.5版好了~ 1 sudo dpkg --purge postgis postgresql-9.1-post ...

  4. libvirt kvm 虚拟机上网 – Bridge桥接

    版权声明:本文由陈煜东原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/90 来源:腾云阁 https://www.qclou ...

  5. Python汉英/英汉翻译(百度API/有道API)

    一.百度API实现 Step1:申请API Key 以前用过BAE,已经有了Api Key,没有的可以去申请 Step2:挺简单,直接看实现的代码吧 ```python #coding:utf-8 i ...

  6. c# Use NAudio Library to Convert MP3 audio into WAV audio(将Mp3格式转换成Wav格式)

    Have you been in need of converting mp3 audios to wav audios?  If so, the skill in this article prov ...

  7. OC开发_Storyboard——NaviationController简单例子

    一个简单的Navigation的例子,demo里面用到了上一个demo的MVC,可以参考下:http://www.cnblogs.com/daomul/p/4426063.html 建立一个Nav其实 ...

  8. SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase, Hana]

    Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-co ...

  9. 全面解析Oracle等待事件的分类、发现及优化

    一.等待事件由来 大家可能有些奇怪,为什么说等待事件,先谈到了指标体系.其实,正是因为指标体系的发展,才导致等待事件的引入.总结一下,Oracle的指标体系,大致经历了下面三个阶段: · 以命中率为主 ...

  10. CSRF攻击详解(转)

    原文:http://www.django-china.cn/topic/580/ 一.CSRF是什么? CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称 ...