poj:1985:Cow Marathon(求树的直径)
Cow Marathon
| Time Limit: 2000MS | Memory Limit: 30000K | |
| Total Submissions: 5496 | Accepted: 2685 | |
| Case Time Limit: 1000MS | ||
Description
Input
Output
Sample Input
7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
Sample Output
52
Hint
分析
求树的直径,可以两遍bfs,求树的直径。
我做的是树形dp。
随便找一个节点把无根树变为有根树,考虑随便找一个根,得到一棵有根树。那么每条路径都有一个根。
code
#include<cstdio>
#include<algorithm>
#include<cstring> using namespace std; const int MAXN = ;
struct Edge{
int to,nxt,w;
}e[];
int head[MAXN],dp[MAXN];
bool vis[MAXN];
int n,m,tot,ans;
char s[]; inline void init()
{
memset(vis,false,sizeof(vis));
memset(dp,,sizeof(dp));
memset(head,,sizeof(head));
tot = ;
ans = ;
}
inline void add_edge(int u,int v,int w)
{
e[++tot].to = v;e[tot].w = w;e[tot].nxt = head[u];
head[u] = tot;
e[++tot].to = u;e[tot].w = w;e[tot].nxt = head[v];
head[v] = tot;
}
void dfs(int u)
{
vis[u] = true;
for (int i=head[u]; i; i=e[i].nxt)
{
int v = e[i].to,w = e[i].w;
if (!vis[v])
{
dfs(v);
ans = max(ans,dp[u]+w+dp[v]);
dp[u] = max(dp[u],dp[v]+w);
}
}
}
int main()
{
while (~scanf("%d%d",&n,&m))
{
init();
for (int x,y,z,i=; i<=m; ++i)
{
scanf("%d%d%d%s",&x,&y,&z,s);
add_edge(x,y,z);
}
dfs();
printf("%d\n",ans);
}
return ;
}
poj:1985:Cow Marathon(求树的直径)的更多相关文章
- POJ 1985 Cow Marathon【树的直径】
题目大意:给你一棵树,要你求树的直径的长度 思路:随便找个点bfs出最长的点,那个点一定是一条直径的起点,再从那个点BFS出最长点即可 以下研究了半天才敢交,1.这题的输入格式遵照poj1984,其实 ...
- poj 1985 Cow Marathon【树的直径裸题】
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 4185 Accepted: 2118 Case ...
- POJ 1985 Cow Marathon(树的直径模板)
http://poj.org/problem?id=1985 题意:给出树,求最远距离. 题意: 树的直径. 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点, ...
- 题解报告: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
题目连接 http://poj.org/problem?id=1985 Cow Marathon Description After hearing about the epidemic of obe ...
- Cow Marathon(树的直径)
传送门 Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5362 Accepted: 2634 ...
- [POJ1985] Cow Marathon 「树的直径」
>传送门< 题意:求树的直径 思路:就是道模板题,两遍dfs就求出来了 Code #include <cstdio> #include <iostream> #in ...
- 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 && POJ 1849 Two(树的直径)
树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...
随机推荐
- zuul忽略表达式
如果有error过滤器,会进入error
- H5的storage(sessionstorage&localStorage)简单存储删除
众所周知,H5的storage有sessionstorage&localStorage,其中他们的共同特点是API相同 下面直接上代码,storage中的存储与删除: <!DOCTYPE ...
- Android自定义组件系列【17】——教你如何高仿微信录音Toast
一.Toast介绍 平时我们在Android开发中会经常用到一个叫Toast的东西,官方解释如下 A toast is a view containing a quick little message ...
- Garmin APP开发之布局
上一章节介绍了garmin app开发的入门,包括garmin-sdk,开发工具的安装部署,文章结尾我们新建了我们的第一个app程序Garmin开发-入门: http://tieba.baidu.co ...
- 获取cell中的button在整个屏幕上的位置
编写cell中得button点击事件 - (IBAction)showButtonClick:(id)sender { UIButton *button = (UIButton *)sender; U ...
- Easy UI 关联表的字段展示
刚接触的easy UI ,发现展示关联表的字段的时候,卡住了 好一段时间,后来通过qq群询问,终于得到答案 实体Record public class Record:Base { public Gui ...
- python(一)
1 python安装 先安装python,之后安装pycharm 创建工程时需要关联解释器路径: 2 python运行 先编译后解释 .py--.pyc文件---解析----结果 .pyc的目的是减少 ...
- LibreOJ #2037. 「SHOI2015」脑洞治疗仪
线段树区间合并问题 恶心... 屠龙宝刀点击就送 #include <cstdio> #define N 200005 struct Segment { int l,r,mid,sum,l ...
- 安装指定版本的minikube
Minikube是什么? Kubernetes集群的安装和部署对于很多初学者来说是一道坎.为了方便大家开发和体验Kubernetes,Kubernetes开源社区提供了可以在本地部署的Minikube ...
- 【vue iview】项目 win10 放在C盘 经常npm install不成功,就是因为 权限问题,把代码目录放到D盘就没事了。
[vue iview]项目 win10 放在C盘 经常npm install不成功,就是因为 权限问题,把代码目录放到D盘就没事了.