BZOJ 3363 POJ 1985 Cow Marathon 树的直径
题目大意:给出一棵树。求两点间的最长距离。
思路:裸地树的直径。两次BFS,第一次随便找一个点宽搜。然后用上次宽搜时最远的点在宽搜。得到的最长距离就是树的直径。
CODE:
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 80010
using namespace std; int points,edges;
int head[MAX],total;
int next[MAX],aim[MAX],length[MAX]; int f[MAX]; char s[10]; inline void Add(int x,int y,int len);
inline void BFS(int start); int main()
{
cin >> points >> edges;
for(int x,y,len,i = 1;i <= edges; ++i) {
scanf("%d%d%d%s",&x,&y,&len,s);
Add(x,y,len),Add(y,x,len);
}
BFS(1);
int _max = 0;
for(int i = 1;i <= points; ++i)
if(f[i] > f[_max])
_max = i;
BFS(_max);
int ans = 0;
for(int i = 1;i <= points; ++i)
if(f[i] > ans)
ans = f[i];
cout << ans << endl;
return 0;
} inline void Add(int x,int y,int len)
{
next[++total] = head[x];
aim[total] = y;
length[total] = len;
head[x] = total;
} inline void BFS(int start)
{
static queue<int> q;
while(!q.empty()) q.pop();
memset(f,0x3f,sizeof(f));
f[0] = f[start] = 0;
q.push(start);
while(!q.empty()) {
int x = q.front(); q.pop();
for(int i = head[x];i;i = next[i])
if(f[aim[i]] > f[x] + length[i]) {
f[aim[i]] = f[x] + length[i];
q.push(aim[i]);
}
}
}
BZOJ 3363 POJ 1985 Cow Marathon 树的直径的更多相关文章
- poj 1985 Cow Marathon 树的直径
题目链接:http://poj.org/problem?id=1985 After hearing about the epidemic of obesity in the USA, Farmer J ...
- poj 1985 Cow Marathon
题目连接 http://poj.org/problem?id=1985 Cow Marathon Description After hearing about the epidemic of obe ...
- poj 1985 Cow Marathon【树的直径裸题】
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 4185 Accepted: 2118 Case ...
- POJ 1985 Cow Marathon && POJ 1849 Two(树的直径)
树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...
- POJ 1985 Cow Marathon(树的直径模板)
http://poj.org/problem?id=1985 题意:给出树,求最远距离. 题意: 树的直径. 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点, ...
- poj:1985:Cow Marathon(求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5496 Accepted: 2685 Case ...
- 题解报告:poj 1985 Cow Marathon(求树的直径)
Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...
- POJ 1985 Cow Marathon (模板题)(树的直径)
<题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...
- POJ 1985 Cow Marathon (树形DP,树的直径)
题意:给定一棵树,然后让你找出它的直径,也就是两点中的最远距离. 析:很明显这是一个树上DP,应该有三种方式,分别是两次DFS,两次BFS,和一次DFS,我只写了后两种. 代码如下: 两次BFS: # ...
随机推荐
- (转)UITextField
//初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, ...
- HDU 6447
YJJ's Salesman Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- python中set()函数的用法
set顾名思义是集合,里面不能包含重复的元素,接收一个list作为参数 list1=[1,2,3,4] s=set(list1) print(s) #逐个遍历 for i in s: print(i) ...
- unittest单元测试(测试报告生成)
自动化测试执行完成之后,我们需要生成测试报告来查看测试结果,使用HTMLTestRunner模块可以直接生产Html格式的报告. 下载地址: http://tungwaiyip.info/softwa ...
- 在后台编辑器Text和Visual切换时,部分代码丢失的解决方法
function fix_tiny_mce_before_init( $in ) { // You can actually debug this without actually needing A ...
- MySQL 子查询优化案例
开发人员给了一个sql ,结构如下delete from B where ID in (select NID from H where guid='xxx'); 内部sql满足条件的结果集只有一条,但 ...
- 如何诊断RAC系统中的'gc cr multi block request'?
'gc cr multi block request' 是RAC数据库上比较常见的一种等待事件,在RAC 上进行全表扫描(Full Table Scan)或者全索引扫描(Index Fast Full ...
- 计算几何 I. 极角
参考资料 hankcs.com: POJ 1981 Circle and Points 题解 aswmtjdsj: POJ 1981 Circle and Points [定长圆覆盖最多点问题] zx ...
- BZOJ 3569 DZY Loves Chinese II ——线性基
[题目分析] 腊鸡题目卡题面. 大概的意思就是给一张无向图,每次删掉其中一些边,问是否联通. 首先想到的是Bitset,可以做到n^2/64.显然过不了. 然而这是lyd在给我们讲线性基的时候的一道题 ...
- VMware虚拟机 NAT模式 配置静态ip
前言:Ubuntu 16.04 VMware虚拟机 NAT模式 配置静态ip,这个问题困扰我好长时间,桥接的静态ip我会了,然而用NAT 的方式配置集群会更好.(NAT 方式客户机之间的通讯不经过路由 ...