C++-HDU2196-Computer-[树的直径]
直径定义:树上的最长路径,不妨设端点分别为s,t
可以证明(感觉):每个点到其最远点必定为s or t,反之亦然
首先,第一次dfs找到s
然后,第二次dfs以s为根找到t
最后,第三次dfs以t为根
比较二三两次的当前点深度可得到答案
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN=,MAXM=;
struct edge{
int u,v,w,next;
edge(int u=,int v=,int w=,int next=):u(u),v(v),w(w),next(next){}
}; edge E[MAXM];
int head[MAXN],fa[MAXN],dis[MAXN],ans[MAXN],cnt,n,s,t,maxdis;
void add(int u,int v,int w){
E[++cnt]=edge(u,v,w,head[u]),head[u]=cnt;
E[++cnt]=edge(v,u,w,head[v]),head[v]=cnt;
} int dfs(int x,int fa){
for(int i=head[x];i;i=E[i].next)
if(E[i].v!=fa){
dis[E[i].v]=max(dis[x]+E[i].w,dis[E[i].v]);
dfs(E[i].v,x);
}
} int search(int root){
int x=root;
maxdis=,memset(dis,,sizeof(dis)),dfs(root,root);
for(int i=;i<=n;i++)if(dis[i]>maxdis)maxdis=dis[i],x=i;
return x;
} int main(){
while(scanf("%d",&n)!=EOF){
cnt=;
memset(head,,sizeof(head));
for(int u=,v,w;u<=n;u++)scanf("%d%d",&v,&w),add(u,v,w);
int s=search();
int t=search(s);
for(int i=;i<=n;i++)ans[i]=dis[i];
search(t);
for(int i=;i<=n;i++)printf("%d\n",max(ans[i],dis[i]));
}
return ;
}
C++-HDU2196-Computer-[树的直径]的更多相关文章
- [hdu2196]Computer树的直径
题意:求树中距离每个节点的最大距离. 解题关键:两次dfs,第一次从下向上dp求出每个节点子树中距离其的最大距离和不在经过最大距离上的子节点上的次大距离(后序遍历),第二次从上而下dp求出其从父节点过 ...
- 【HDU 2196】 Computer(树的直径)
[HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...
- hdu 2196 Computer 树的直径
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- HDOJ 2196 Computer 树的直径
由树的直径定义可得,树上随意一点到树的直径上的两个端点之中的一个的距离是最长的... 三遍BFS求树的直径并预处理距离....... Computer Time Limit: 1000/1000 MS ...
- codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径
题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...
- codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点
J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...
- HDU 2196.Computer 树形dp 树的直径
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- [J]computer network tarjan边双联通分量+树的直径
https://odzkskevi.qnssl.com/b660f16d70db1969261cd8b11235ec99?v=1537580031 [2012-2013 ACM Central Reg ...
- computer(树形dp || 树的直径)
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu2196 树的直径 + bfs
//Accepted 740 KB 15 ms //树的直径 //距离一个顶点最远的点一定是树的直径的一个端点 #include <cstdio> #include <cstring ...
随机推荐
- selenium等待机制
等待机制 因为你要查找的标签由于网速等原因迟迟没有加载出来,你就直接获取这个标签,很明显是报错,现有的简单粗暴的解决办法就是time.sleep(3),睡几秒,也就是设置线程等待,等这个标签加载出 ...
- so the first day
welcome to the world it sucks but you gona love it
- jvm编译器的优化
1.对byte.short.char赋值时,若右边范围没有超过左边类型的最大表达范围则会自动隐式的加上(byte).(short).(char)强制转换:若右边范围超过了左边类型的最大表达范围则编译失 ...
- 跳跃【BFS】
From 牛客网:https://ac.nowcoder.com/acm/problem/25160
- 中间件c10k问题
中间件c10k问题 没有使用iocp/epoll/kqueue通讯的中间件,中间件就算部署在拥有多核CPU的强大服务器上,最头痛的问题是C10K问题. 中间件没有办法通过优化程序,提升CPU利用率来处 ...
- C# WPF遮罩对话框(Popup Message Overlay/ Dialog Host)
时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...
- adworld MISC002 | Linux的挂载文件系统的运用
EXT3是第三代扩展文件系统(英语:Third extended filesystem,缩写为ext3),是一个日志文件系统,常用于Linux操作系统. Plan 1: 直接将附件使用mount命令挂 ...
- day03_2spring3
SSH整合(续) 一.spring整合hibernate:有hibernate.cfg.xml 前提:导入jar包,在前面已经介绍了jar包的整合,我们只需要将整合的所有jar包导进去即可. 1.创建 ...
- HDU-1506 Largest Rectangle in a Histogram【单调栈】
Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...
- window.location.herf传值问题
各个值之间用&&&&&&连接 新版本的tomcat不支持其他字符,需要通过encodeURIComponent编码 变量名数字后不能直接加字母 such ...