Computer(hdu 2196)
题意:给出一棵树,求出每个点与距离它最远的点的距离。
/*
树形DP
先把无根树转为有根树,对于一个节点i来说,与它相距最远的点有两种可能,一是在它的子树中,二是不在,我们分别用f[i][0]和f[i][1]来表示。
f[i][0]很好求,从子节点向它的父亲递推就可以了。
关键在于求f[i][1],我们发现f[i][1]可能有两种情况,一是来自它父亲的子树,而是来自它父亲的f[j][1],然后判断一下就好了。
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define N 10010
#define lon long long
using namespace std;
int head[N],vis[N],n,cnt;
lon f[N][];
struct node{int v,w,pre;}e[N*];
void add(int u,int v,int w){
e[++cnt].v=v;e[cnt].w=w;e[cnt].pre=head[u];head[u]=cnt;
e[++cnt].v=u;e[cnt].w=w;e[cnt].pre=head[v];head[v]=cnt;
}
lon dfs1(int u){
vis[u]=;
for(int i=head[u];i;i=e[i].pre){
if(vis[e[i].v]) continue;
f[u][]=max(f[u][],dfs1(e[i].v)+e[i].w);
}
return f[u][];
}
void dfs2(int u){
vis[u]=;
lon max1=,max2=;int v1,v2;
for(int i=head[u];i;i=e[i].pre){
int v=e[i].v,w=e[i].w;
if(vis[v]) continue;
if(f[v][]+w>max1){
max2=max1;v2=v1;
max1=f[v][]+w;v1=v;
}
else if(f[v][]+w>max2){
max2=f[v][]+w;v2=v;
}
}
if(u!=){
if(f[u][]>max1){
max2=max1;v2=v1;
max1=f[u][];v1=-;
}
else if(f[u][]>max2){
max2=f[u][];v2=-;
}
}
for(int i=head[u];i;i=e[i].pre){
int v=e[i].v;
if(vis[v]) continue;
if(v1!=v) f[v][]=max1+e[i].w;
else f[v][]=max2+e[i].w;
dfs2(e[i].v);
}
}
void work(){
for(int u=;u<=n;u++){
int v,w;scanf("%d%d",&v,&w);
add(u,v,w);
}
memset(vis,,sizeof(vis));
dfs1();
memset(vis,,sizeof(vis));
dfs2();
for(int i=;i<=n;i++)
cout<<max(f[i][],f[i][])<<endl;
}
int main(){
freopen("jh.in","r",stdin);
while(scanf("%d",&n)!=EOF){
memset(head,,sizeof(head));
memset(f,,sizeof(f));
cnt=;
work();
}
return ;
}
Computer(hdu 2196)的更多相关文章
- BestCoder25 1001.Harry and Magical Computer(hdu 5154) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5154 题目意思:有 n 门 processes(编号依次为1,2,...,n),然后给出 m 种关系: ...
- 2道acm编程题(2014):1.编写一个浏览器输入输出(hdu acm1088);2.encoding(hdu1020)
//1088(参考博客:http://blog.csdn.net/libin56842/article/details/8950688)//1.编写一个浏览器输入输出(hdu acm1088)://思 ...
- 树形dp(B - Computer HDU - 2196 )
题目链接:https://cn.vjudge.net/contest/277955#problem/B 题目大意:首先输入n代表有n个电脑,然后再输入n-1行,每一行输入两个数,t1,t2.代表第(i ...
- Bestcoder13 1003.Find Sequence(hdu 5064) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5064 题目意思:给出n个数:a1, a2, ..., an,然后需要从中找出一个最长的序列 b1, b ...
- 2013 多校联合 F Magic Ball Game (hdu 4605)
http://acm.hdu.edu.cn/showproblem.php?pid=4605 Magic Ball Game Time Limit: 10000/5000 MS (Java/Other ...
- (多线程dp)Matrix (hdu 2686)
http://acm.hdu.edu.cn/showproblem.php?pid=2686 Problem Description Yifenfei very like play a num ...
- War Chess (hdu 3345)
http://acm.hdu.edu.cn/showproblem.php?pid=3345 Problem Description War chess is hh's favorite game:I ...
- 2012年长春网络赛(hdu命题)
为迎接9月14号hdu命题的长春网络赛 ACM弱校的弱菜,苦逼的在机房(感谢有你)呻吟几声: 1.对于本次网络赛,本校一共6名正式队员,训练靠的是完全的自主学习意识 2.对于网络赛的群殴模式,想竞争现 ...
- BestCoder Round #69 (div.2) Baby Ming and Weight lifting(hdu 5610)
Baby Ming and Weight lifting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
随机推荐
- 使用MyEclipse/Eclipse修改项目名称报Can't convert argument: null!
报错: java.lang.IllegalArgumentException: Can't convert argument: null! 方法/步骤 报错原因:使用MyEclipse修改项目名 ...
- php-语言参考-基本语法3.1
一,PHP代码的开始和结束标记 1,<?php 和 ?> //重点 2,<script language="php"> 和 </script> ...
- anaconda+jupyter notebook 安装配置
安装Anaconda 从清华大学开源软件镜像站选择合适自己的版本 wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda ...
- python中的列表内置方法小结
#!/usr/local/bin/python3 # -*- coding:utf-8 -*- ''' names=['zhangyu','mahongyan','zhangguobin','shac ...
- MyFirstDay(附6篇python亲历面试题)
一直以来都是在看别人写的内容,学习前辈们的经验,总感觉自己好像没有什么值得拿出来分享和交流的知识,最近在准备换工作(python后端开发),坐标上海,2019年3月,半个月面了6家(感觉效率是真不高. ...
- 为 vsftpd 启动 vsftpd:500 OOPS: SSL: cannot load RSA&nb
博主在配置ftp服务器的时候 自己生成的CA,然后认证自己的私钥文件 一切配置都是没有问题的,然后重新启动服务器 service vsftpd restart 出现以下错误 为 vsftpd 启 ...
- POJ:2395-Out of Hay
Out of Hay Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18780 Accepted: 7414 Descripti ...
- 使用Matrix-tree与它的行列式来解决生成树计数问题
我又把Matrix写错啦 这东西讲课的时候竟然一笔带过了,淦 好吧这东西我不会证 那我们来愉快的看结论吧 啦啦啦 预备工作 你有一个 $ n $ 个点的图 比如说 5 /|\ / | \ 2--1-- ...
- UIView和CALayer是什么关系?
UIView显示在屏幕上归功于CALayer,通过调用drawRect方法来渲染自身的内容,调节CALayer属性可以调整UIView的外观,UIView继承自UIResponder,比起CALaye ...
- 《Cracking the Coding Interview》——第12章:测试——题目6
2014-04-25 00:53 题目:你要如何测试一个分布式银行系统的ATM机? 解法:ATM是Automatic Teller Machine,取钱的.我想了半天,没找到什么很清晰的思路,也许是因 ...