题目链接:http://poj.org/problem?id=2631

题意:给出一棵树的两边结点以及权重,就这条路上的最长路。

思路:求树的直径。

这里给出树的直径的证明:

  主要是利用了反证法:

  假设 s-t这条路径为树的直径,或者称为树上的最长路
  现有结论,从任意一点u出发搜到的最远的点一定是s、t中的一点,然后再从这个最远点开始搜,就可以搜到另一个最长路的端点,即用两遍广搜就可以找出树的最长路
  证明:

     1.设u为s-t路径上的一点,结论显然成立,否则设搜到的最远点为T则   dis(u,T) >dis(u,s)     且  dis(u,T)>dis(u,t)   则最长路不是s-t了,与假设矛盾
     2.设u不为s-t路径上的点
        首先明确,假如u走到了s-t路径上的一点,那么接下来的路径肯定都在s-t上了,而且终点为s或t,在1中已经证明过了
        所以现在又有两种情况了:
          1:u走到了s-t路径上的某点,假设为X,最后肯定走到某个端点,假设是t ,则路径总长度为dis(u,X)+dis(X,t)
          2:u走到最远点的路径u-T与s-t无交点,则dis(u-T) >dis(u,X)+dis(X,t);显然,如果这个式子成立,
          则dis(u,T)+dis(s,X)+dis(u,X)>dis(s,X)+dis(X,t)=dis(s,t)最长路不是s-t矛盾 (见下图)
 
求树的直径用2次bfd和2次dfs都可以。
 
dfs代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
const int maxn=1e5+;
const int INF=0x3f3f3f3f; struct Edge
{
int v,l;
int next;
} edge[maxn<<]; int vit[maxn],d[maxn];
int head[maxn],k;
int node,ans; void init()
{
k=;
memset(head,-,sizeof(head));
} void addedge(int u,int v,int l)
{
edge[k].v=v;
edge[k].l=l;
edge[k].next=head[u];
head[u]=k++; edge[k].v=u;
edge[k].l=l;
edge[k].next=head[v];
head[v]=k++;
} void dfs(int u,int t)
{
for(int i=head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].v;
if(vit[v]==)
{
vit[v]=;
d[v]=t+edge[i].l;
if(d[v]>ans)
{
ans=d[v];
node=v;
}
dfs(v,d[v]);
}
}
} int main()
{
//freopen("in.txt","r",stdin);
init();
int l,r,len;
while(scanf("%d%d%d",&l,&r,&len)==)
{
addedge(l,r,len);
} memset(vit,,sizeof(vit));
vit[]=;
ans=;
dfs(,); memset(vit,,sizeof(vit));
vit[node]=;
ans=;
dfs(node,); printf("%d\n",ans); return ;
}

bfs代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
#define ll long long
const int maxn=1e5+;
const int INF=0x3f3f3f3f; struct Edge
{
int v,l;
int next;
} edge[maxn<<]; int vit[maxn],d[maxn];
int head[maxn],k;
int node,ans; void init()
{
k=;
memset(head,-,sizeof(head));
} void addedge(int u,int v,int l)
{
edge[k].v=v;
edge[k].l=l;
edge[k].next=head[u];
head[u]=k++; edge[k].v=u;
edge[k].l=l;
edge[k].next=head[v];
head[v]=k++;
} void bfs(int p)
{
queue<int>q;
vit[p]=;
q.push(p);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].v;
if(vit[v]==)
{
d[v]=d[u]+edge[i].l;
vit[v]=;
q.push(v);
if(d[v]>ans)
{
ans=d[v];
node=v;
}
}
}
}
} int main()
{
//freopen("in.txt","r",stdin);
init();
int l,r,len;
while(scanf("%d%d%d",&l,&r,&len)==)
{
addedge(l,r,len);
} memset(vit,,sizeof(vit));
memset(d,,sizeof(d));
ans=;
bfs(); memset(vit,,sizeof(vit));
d[node]=;
ans=;
bfs(node); printf("%d\n",ans); return ;
}

poj2631 求树的直径裸题的更多相关文章

  1. lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】

    1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

  2. poj 2631 Roads in the North【树的直径裸题】

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2359   Accepted: 115 ...

  3. poj 1985 Cow Marathon【树的直径裸题】

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4185   Accepted: 2118 Case ...

  4. POJ3107Godfather(求树的重心裸题)

    Last years Chicago was full of gangster fights and strange murders. The chief of the police got real ...

  5. LightOJ--1094-- Farthest Nodes in a Tree(树的直径裸题)

    Farthest Nodes in a Tree Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu S ...

  6. poj1985 / poj2631(树的直径)

    poj1985 Cow Marathon 树的直径裸题 树的直径的一般求法: 任意一点为起点,dfs/bfs找出与它最远的点$u$ 以$u$为起点,dfs/bfs找出与它最远的点$v$ 则$d(u,v ...

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

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

  8. UESTC 1591 An easy problem A【线段树点更新裸题】

    An easy problem A Time Limit: 2000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others ...

  9. 求树的直径+并查集(bfs,dfs都可以)hdu4514

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 这题主要是叫我们求出树的直径,在求树的直径之前要先判断一下有没有环 树的直径指的就是一棵树上面距 ...

随机推荐

  1. How to stop pycharm show files in project in red color?

    You can change the file color to whatever you want. File > Settings > Editor > Colors&F ...

  2. maridb(mysql) debian-sys-maint用户说明

    debian-sys-maint中Debian系统对MySQL维护用的,可以理解为通过系统的某个“非常规”程序对Mysql进行备份恢复等行为时,改程序所使用的登录Mysql的账户. 这个debian- ...

  3. 关于背景图相对父容器垂直居中问题 —— vertical-align 和 line-height 之间的区别

       html css <div class="register-wrapper"> <div class="register"> &l ...

  4. .NET简谈事务、分布式事务处理

    http://www.cnblogs.com/wangiqngpei557/archive/2011/12/22/2298500.html

  5. MySQL 5.6 新参数对binlog日志量的优化

    数据库版本:5.6.* 1.row日志image类型 参数binlog_row_image 控制着这种image类型,默认为FULL(log all columns),即记录before&af ...

  6. jquery实现 复选框 全选

    $("#checkAll").change(function () { $(this).closest("table") .find(":checkb ...

  7. Ubuntu 12.4 Apache2 安装教程

    一.更新操作系统 sudo apt-get update && sudo apt-get upgrade 二.安装Apache及依赖 sudo apt-get install apac ...

  8. cf723d Lakes in Berland

    The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × 1. Each cel ...

  9. MVVM开发模式简单实例MVVM Demo【续】

    本文将接着上篇文章,介绍一下三点:(Universal App) 1.将添加Product集合,绑定到列表 2.给点击ListBox的添加选项改变时的事件(要附加依赖属性,和Button点击事件不同) ...

  10. MongoDB系列一:CentOS7.2下安装mongoDB3.2.8

    最近在又在倒腾MongoDB,把安装配置的相关命令贴出来 1.下载 wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70- ...