POJ 2631 Roads in the North (模板题)(树的直径)
<题目链接>
题目大意:
求一颗带权树上任意两点的最远路径长度。
解题分析:
裸的树的直径,可由树形DP和DFS、BFS求解,下面介绍的是BFS解法。
在树上跑两遍BFS即可,第一遍BFS以任意点为起点,此时得到的离它距离最远的点为树的直径上的端点之一,然后再以这个端点为起点,跑一遍BFS,此时离它最远的点为树直径的另一个端点,同时,它们之间的距离即为树的直径。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int M=1e4+; struct Edge{
int to,val,next;
}edge[M<<];
int vis[M],d[M];
int head[M],cnt;
int res,ans;
void init(){
cnt=;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,int w){
edge[++cnt].to=v,edge[cnt].val=w;
edge[cnt].next=head[u],head[u]=cnt;
}
void bfs(int s){
memset(vis,,sizeof(vis));
queue<int>que;
vis[s]=;d[s]=;
que.push(s);
while(!que.empty()){
int u=que.front();
que.pop();
for(int i=head[u]; i!=-; i=edge[i].next){
int v=edge[i].to;
if(!vis[v]){
d[v]=d[u]+edge[i].val;
vis[v]=;
que.push(v);
if(d[v]>ans) //更新最远距离
ans=d[v],res=v;
}
}
}
}
int main(){
init();
int u,v,w;
while(scanf("%d%d%d",&u,&v,&w)!=EOF)
addedge(u,v,w),addedge(v,u,w);
ans=;
bfs(); //先得到离1距离最远的节点(该节点为树的直径那两个节点之一)
bfs(res); //然后再跑一遍BFS,得到直径的另一个端点
printf("%d\n",ans);
return ;
}
2018-11-07
POJ 2631 Roads in the North (模板题)(树的直径)的更多相关文章
- POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)
题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in ...
- poj 2631 Roads in the North (自由树的直径)
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4513 Accepted: 215 ...
- POJ 2631 Roads in the North (求树的直径)
Description Building and maintaining roads among communities in the far North is an expensive busine ...
- POJ 2631 Roads in the North(树的直径)
POJ 2631 Roads in the North(树的直径) http://poj.org/problem? id=2631 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u ...
- poj 2631 Roads in the North
题目连接 http://poj.org/problem?id=2631 Roads in the North Description Building and maintaining roads am ...
- poj 2631 Roads in the North【树的直径裸题】
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2359 Accepted: 115 ...
- POJ 1985 Cow Marathon (模板题)(树的直径)
<题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...
- 题解报告:poj 2631 Roads in the North(最长链)
Description Building and maintaining roads among communities in the far North is an expensive busine ...
- POJ 2631 Roads in the North (树的直径)
题意: 给定一棵树, 求树的直径. 分析: 两种方法: 1.两次bfs, 第一次求出最远的点, 第二次求该点的最远距离就是直径. 2.同hdu2196的第一次dfs, 求出每个节点到子树的最长距离和次 ...
随机推荐
- python1113
点点滴滴才可以来开距离,人与人的差距是在点点滴滴中拉开的 break 语句可以跳出 for 和 while 的循环体的当前循环 continue语句被用来告诉Python跳过当前循环块中的剩余语句,然 ...
- 《精通Oracle SQL(第2版)》PDF
一:下载途径 二:图书图样 三:目录 第1章 SQL核心 1.1 SQL语言 1.2 数据库的接口 1.3 SQL*Plus回顾 1.3.1 连接到数据库 1.3.2 配置SQL*Plus环境 1.3 ...
- Confluence 6 内存使用和需求和一些问题
系统备份和恢复 Confluence 的备份和恢复是与数据库中数据量的大小有关.这个操作可能会对 Confluence 的性能产生很多关键性的影响并且大量消耗内存.如果你在 Confluence 的 ...
- Confluence 6 配置草稿保存的时间
这个设置仅仅应用到 Confluence 6.0 及后续版本中,如果你选择 禁用(disable )collaborative editing. 当协同编辑被启用后,我们将会保存所有的修改. 当协同编 ...
- CentOS 7 部署 Spring Boot
Spring Boot 内嵌了tomcat .我们可以将Boot打成 jar 包丢到服务器上运行才行. Spring Boot已经帮我们打理好了这一切,如果项目是继承自 spring-boot-sta ...
- js中的 !! 和 ! 的区别
简单描述:看老大提交的代码,发现了一个事情,有一处是连着两个! 然后就把我弄纳闷了,果断找度娘,原来如此.上代码 代码: if(!!s && s.length > 0){ /* ...
- Python实操二
实操一: 1.用map来处理字符串列表啊,把列表中所有人都变成sb,比方alex_sb name=['alex','wupeiqi','yuanhao'] name=['alex','wupeiqi' ...
- dbcp连接池出现的问题java.lang.AbstractMethodError: com.mysql.jdbc.Connection.isValid(I)Z
解决方案:mysql-connector 版本为 5.0.4 ,那么对应的 dbcp 和 pool 版本应该为 1.4 和 1.6 . 5.0.4 不应该使用 2.0 及以上版本的 dbcp 和 ...
- zoj3659
#include<iostream> #include<algorithm> #include<cstring> #define ll long long #inc ...
- 使用python调用shell判断当前进程是否存在
使用subprocess模块判断当前进程是否存在 #! /usr/bin/env python import subprocess res = subprocess.Popen(r'ps -ef |g ...