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: # ...
随机推荐
- HUB、Switch、Router在OSI模型层次信息
序 (HUB)集线器工作在局域网(LAN)环境,像网卡一样,应用于OSI参考模型第一层,因此又被称为物理层设备. Switch交换机工作在OSI第2层数据链路层 Router路由器工作在OSI第3层网 ...
- Hive元数据启动失败
Caused by: java.net.ConnectException: Connection refused (Connection refused) at java.net.PlainSocke ...
- pytorch将cpu训练好的模型参数load到gpu上,或者gpu->cpu上
假设我们只保存了模型的参数(model.state_dict())到文件名为modelparameters.pth, model = Net() 1. cpu -> cpu或者gpu -> ...
- 紫书第五章训练2 F - Compound Words
F - Compound Words You are to find all the two-word compound words in a dictionary. A two-word compo ...
- BZOJ 4516 [Sdoi2016]生成魔咒 ——后缀自动机
本质不同的字串,考虑SA的做法,比较弱,貌似不会. 好吧,只好用SAM了,由于后缀自动机的状态最简的性质, 所有不同的字串就是∑l[i]-l[fa[i]], 然后后缀自动机是可以在线的,然后维护一下就 ...
- 【单调队列】poj 2823 Sliding Window
http://poj.org/problem?id=2823 [题意] 给定一个长度为n的序列,求长度为k的滑窗内的最大值和最小值 [思路] 裸的单调队列 注意用C++提交,不然会T,orz我用G++ ...
- 「SDOI2010」古代猪文(bzoj1951)
题目写了一大堆背景. 一句话题意就是求 $q^{\sum_{d|n}C_{n}^{d}} \mod 999911659$. 因为$n$是质数,只有当$q$是$n$的倍数时(此题数据范围原因,最多$q= ...
- 转载: 找不到MSVCR90.dll、Debug vs Release及cppLapack相关
今天调试程序时出现了,找不到MSCVR90.dll的错误,最好查找到了解决办法,原文链接如下: http://hi.baidu.com/wpzhao/blog/item/72dc08f77ce9b ...
- STL学习笔记(五) 算法
条款30:确保目标区间足够大 条款31:了解各种与排序有关的选择 //使用unaryPred划分输入序列,使得unaryPred为真的元素放在序列开头 partition(beg, end, unar ...
- 【Educational Codeforces Round 49 (Rated for Div. 2) 】
A:https://www.cnblogs.com/myx12345/p/9843826.html B:https://www.cnblogs.com/myx12345/p/9843869.html ...