POJ 1985 Cow Marathon (树形DP,树的直径)
题意:给定一棵树,然后让你找出它的直径,也就是两点中的最远距离。
析:很明显这是一个树上DP,应该有三种方式,分别是两次DFS,两次BFS,和一次DFS,我只写了后两种。
代码如下:
两次BFS:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue> using namespace std;
const int maxn = 1e5 + 5;
int d[maxn];
bool vis[maxn], vvis[maxn];//vis是BFS的标记,vvis是总的标记
vector<int> G[maxn], w[maxn];
//思路就是先在树上找一个点,然后从这个点开始找,找最远的点,
//然后两从最远的点找最远的点,这个所有点中的最大值就是树的直径
//d[i]表示到结点 i 的最长距离
int bfs(int root){
memset(vis, false, sizeof(vis));
memset(d, 0, sizeof(d));
queue<int> q;
q.push(root);
int ans = root, m = 0;
vis[root] = vvis[root] = true;
while(!q.empty()){
root = q.front(); q.pop();
for(int i = 0; i < G[root].size(); ++i){
int u = G[root][i];
if(vis[u]) continue;
q.push(u);
vis[u] = vvis[u] = true;
d[u] = d[root] + w[root][i];
if(d[u] > m){
ans = u;
m = d[u];
}
}
}
return ans;
} void init(int n){
for(int i = 1; i <= n; ++i){
G[i].clear();
w[i].clear();
vvis[i] = false;
}
} int main(){
int n, m, u, v, l;
char ch;
while(cin >> n >> m){
init(n);
while(m--){
scanf("%d %d %d %c", &u, &v, &l, &ch);
G[u].push_back(v); w[u].push_back(l);
G[v].push_back(u); w[v].push_back(l);
} int ans = 0;
for(int i = 1; i <= n; ++i)
if(!vvis[i]) ans = max(ans, d[bfs(bfs(i))]);//两次BFS,第一次是找最远的点,第二次是找最远点的最远点
cout << ans << endl;
}
return 0;
}
一次DFS:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue> using namespace std;
const int maxn = 1e5 + 5;
int f[maxn], g[maxn], ll[maxn];
vector<int> G[maxn], w[maxn];
//思路主要是找一个点的最远点加次远点就是树的直径 int dfs(int root, int fa){
if(f[root] != -1) return f[root];
if(!G[root].size()) return f[root] = 0;
int m = 0, ans = root;
for(int i = 0; i < G[root].size(); ++i){
int u = G[root][i];
if(u == fa) continue;
if(dfs(u, root) + w[root][i] > m){
m = f[u] + w[root][i];
ans = u;
}
} ll[root] = ans; int mm = 0;
for(int i = 0; i < G[root].size(); ++i){
int u = G[root][i];
if(u == fa) continue;
if(f[u] + w[root][i] > mm && u != ll[root])
mm = f[u] + w[root][i];
}
g[root] = mm;
return f[root] = m;
} void init(int n){
for(int i = 1; i <= n; ++i){
G[i].clear();
w[i].clear();
f[i] = g[i] = ll[i] = -1;
}
} int main(){
int n, m, u, v, l;
char ch;
while(cin >> n >> m){
init(n);
while(m--){
scanf("%d %d %d %c", &u, &v, &l, &ch);
G[u].push_back(v); w[u].push_back(l);
G[v].push_back(u); w[v].push_back(l);
} int ans = 0;
for(int i = 1; i <= n; ++i)
if(f[i] == -1) dfs(i, -1);
for(int i = 1; i <= n; ++i) ans = max(ans, f[i]+g[i]);
cout << ans << endl;
}
return 0;
}
POJ 1985 Cow Marathon (树形DP,树的直径)的更多相关文章
- poj:1985:Cow Marathon(求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5496 Accepted: 2685 Case ...
- POJ 1985 Cow Marathon (模板题)(树的直径)
<题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...
- 题解报告: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 (求树的直径)
Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...
- POJ 3162.Walking Race 树形dp 树的直径
Walking Race Time Limit: 10000MS Memory Limit: 131072K Total Submissions: 4123 Accepted: 1029 Ca ...
- poj 1985 Cow Marathon
题目连接 http://poj.org/problem?id=1985 Cow Marathon Description After hearing about the epidemic of obe ...
- 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【树的直径裸题】
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出来离他最远的一 ...
随机推荐
- ArraySort--冒泡排序、选择排序、插入排序工具类demo
public class ArraySort { private long[] a; private int nElems; public ArraySort(int max){ a=new long ...
- BGP中IBGP和EBGP的区别和联系
我们知道,在自治系统内部使用IGP路由协议:而在不同自治系统之间使用BGP路由协议(严格来讲,BGP不是路由协议).BGP产生的原因是为了在不同自治系统(AS)之间进行路由转发,而其中又提出了EBGP ...
- Pymol
如何用Pymol做出那些美呆的结构图(基础篇) 2016-10-31 翾园 摘自 BioEngX生化... 阅 1079 转 6 转藏到我的图书馆 微信分享: 摘自微信公众号:BioE ...
- CentOS7.6安装Redis
官网下载地址:https://redis.io/download 第一步:软件下载安装 进行安装目录:cd /opt/software (如果目录不存在,请先创建目录) 下载二进制包:wget htt ...
- numpy的通用函数
通用函数:快速的元素级数组函数 通用函数是一种对ndarry中的数据执行元素级运算的函数,可以看作是简单函数(接受一个或多个标量值,并产生一个或多个标量值)的矢量化包装器. 一元func: abs丶f ...
- 手把手教你使用node-inspector调试nodejs
最近再看nodejs,这个东西是运行在服务端的,也就是说我们在客户端看不到相应的js代码,那么怎么调试了?目前主流的方法有三种.第一是采用node-inspector.第二种采用nodejs内置的调试 ...
- FTP服务器(SOCKET)返回异常 500 Command not understood
出现着这样的问题,一般是NLST中的参数包含特殊字符,如"\n",所以在发送SOCKET命令时,一定要检查命令参数的合法性.
- 学习 Python 编程的 19 个资源 (转)
学习 Python 编程的 19 个资源 2018-01-07 数据与算法之美 编译:wzhvictor,英文:codecondo segmentfault.com/a/119000000418731 ...
- Zabbix 监控页面中文乱码
问题描述: 如题,我相信大多数人都遇到过这个问题,Zabbix 监控图 中文乱码. 解决这个问题也很简单:( Zabbix 网页目录中缺少字体 ) 1.打开 Windows 的 C:\Windows\ ...
- Excel 数字处理
说明 最近在做一个比较小型的网站,需要批量导入注册用户.用户的信息写在一张excel表格里面. 所以就需要读取excel.所以就记录下遇到的问题,以及以后查看. 相关技术 使用的POI解析Excel需 ...