POJ 1985
求一棵树内最远的两点,DFS,顺便记录以某节点为根内最远的两点的距离,返回最远点的距离。其实是DP。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cctype>
#include <algorithm>
#define LL unsigned __int64
using namespace std; const int N= ; struct Edge{
int u,v,c;
int next;
}edge[N];
int head[N],n,m,tot,ans; void addedge(int u,int v,int c){
edge[tot].u=u;
edge[tot].v=v;
edge[tot].c=c;
edge[tot].next=head[u];
head[u]=tot++;
} int dfs(int u,int f){
int max1=,max2=,v,tmp;
for(int e=head[u];e!=-;e=edge[e].next){
v=edge[e].v;
if(v!=f){
tmp=edge[e].c;
tmp+=dfs(v,u);
if(tmp>max1){
max2=max1;
max1=tmp;
}
else if(tmp>max2){
max2=tmp;
}
}
}
if(max1+max2>ans)
ans=max1+max2;
return max1;
} int main(){
int u,v,c; char t;
while(scanf("%d%d",&n,&m)!=EOF){
tot=; ans=;
memset(head,-,sizeof(head));
for(int i=;i<=m;i++){
scanf("%d %d %d %c",&u,&v,&c,&t);
// cout<<u<<v<<c<<endl;
addedge(u,v,c);
addedge(v,u,c);
}
dfs(,-);
printf("%d\n",ans);
}
return ;
}
POJ 1985的更多相关文章
- POJ 1985 Cow Marathon && POJ 1849 Two(树的直径)
树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...
- 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(树的直径模板)
http://poj.org/problem?id=1985 题意:给出树,求最远距离. 题意: 树的直径. 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点, ...
- 树的最长链-POJ 1985 树的直径(最长链)+牛客小白月赛6-桃花
求树直径的方法在此转载一下大佬们的分析: 可以随便选择一个点开始进行bfs或者dfs,从而找到离该点最远的那个点(可以证明,离树上任意一点最远的点一定是树的某条直径的两端点之一:树的直径:树上的最长简 ...
- poj 1985 Cow Marathon【树的直径裸题】
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 4185 Accepted: 2118 Case ...
- POJ 1985 Cow Marathon (模板题)(树的直径)
<题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...
- POJ 1985 Cow Marathon (树形DP,树的直径)
题意:给定一棵树,然后让你找出它的直径,也就是两点中的最远距离. 析:很明显这是一个树上DP,应该有三种方式,分别是两次DFS,两次BFS,和一次DFS,我只写了后两种. 代码如下: 两次BFS: # ...
- POJ 1985.Cow Marathon-树的直径-树的直径模板(BFS、DFS(vector存图)、DFS(前向星存图))
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 7536 Accepted: 3559 Case ...
- poj:1985:Cow Marathon(求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5496 Accepted: 2685 Case ...
随机推荐
- 在linux查看内存的大小
用free -m查看的结果: # free -m total used free shared buffers cached Mem: ...
- yii引入js文件
作者:zccst 四.在视图层(../views/..)添加CSS文件或JavaScript文件 Yii::app()->clientScript->registerScriptFile( ...
- SpringAOP使用注解实现5种通知类型
spring aop的5种通知类型都有 Before前置通知 AfterReturning后置通知 Around环绕通知 AfterThrowing异常通知 After最终通知 首先创建接口和实现类 ...
- POJ 3114 Tarjan+Dijkstra
题意: 间谍在战争期间想要传递一份谍报回国,谍报可以在邮局之间传递,但这种传递是单向的,并且会少耗一些时间.但是如果两个邮局在同一个国家的话,那么谍报在这两个邮局之间传递是不消耗时间的.如果几个邮局发 ...
- java selenium启动火狐浏览器报错:Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: VISTA Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:14.666Z
Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: VISTA Build in ...
- 可变长度参数列表(Stering...aaa)
- swift-自定义TabBar工具栏
class EBTAppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(ap ...
- checked、disabled在原生、jquery、vue下不同写法
以下是原生和jquery <!DOCTYPE html> <html> <head> <meta http-equiv="Content ...
- Java多线程中常见的几个问题
我们都知道,在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口. 1.进程和线程的区别是什么? 进程是执行着的应用程序,而线程是进程内部的一个执行序列. ...
- SLAM: Ubuntu14.04_Kylin安装ROS-Indigo
参考连接:ROS-Indigo版在Ubuntu上的安装第一步: 软件源配置 1. 增加下载源(增加ubuntu版的ros数据仓库,即下载源)(通用指令适合任何版本的ros) sudo sh -c 'e ...