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 ...
随机推荐
- qt creator源码全方面分析(1)
目录介绍 首先我们对软件源代码根目录下的各个重要文件(夹)做一个简单的介绍,对整体有一个大概的了解. 下面对目录及其内容做一个大概的初步的介绍,后面我尽量按照目录顺序进行依次介绍,当然可能会有一些交叉 ...
- 文件流之输入输出(类似于freopen重定向)
利用标准文件操作函数进行数据的输入输出,所用函数均在stdio.h中,类似于freopen重定向文件. 该方法的思路是: (1)建立文件指针 (2)打开文件,将文件指针指向打开的文件,并决定打开的文件 ...
- claim、claimsidentity、claimsprincipal
Claim表示一个声明单元,它用来组成ClaimsIdentity.ClaimsIdentity表示一个证件,例如身份证,身份证上面的名字表示一个Claim,身份证号也表示一个Claim,所有这些Cl ...
- Navigation Nightmare POJ - 1984 带权并查集
#include<iostream> #include<cmath> #include<algorithm> using namespace std; ; // 东 ...
- 每天进步一点点------Modelsim添加Xilinx仿真库的详细步骤
Modelsim,可以选型SE和XE两个版本.Modelsim XE可以直接被ISE调用,而Modelsim SE需要手动添加仿真库.但SE版和OEM版在功能和性能方面有较大差别,比如对于大家都关心的 ...
- python3练习100题——039
原题链接:http://www.runoob.com/python/python-exercise-example39.html 题目:有一个已经排好序的数组.现输入一个数,要求按原来的规律将它插入数 ...
- Ubuntu 16 服务器配置PHP+MySQL+Apache环境
一.获取软件包资源并进行资源更新 apt是Ubuntu上默认的软件包管理器,使用它可以很容易进行各种软件安装,而且会自动帮你安装可能需要的依赖关系. 另一个强大的软件管理工具是 dpkg,可以用于安装 ...
- 缓存 - 数据缓存 - IndexedDB - Dexie.js
Classes Dexie DexieError Collection and():Add JS based criteria to collection(向集合添加基于JS的条件) delete() ...
- LED And Incandescent, Who Is Suitable For Holiday Lighting?
Fast-fire advantages of LED lighting: Eco-friendly-LEDs are not made of toxic chemicals, such as mer ...
- js替换从excel复制的文本的换行
var newStr=oldStr.replace(/\n/g,','); 该代码即可将文本中的换行替换为, oldStr为从excel复制过来的值 newStr为替换后的值