题意:给出一棵树,求出每个点与距离它最远的点的距离。

/*
树形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)的更多相关文章

  1. BestCoder25 1001.Harry and Magical Computer(hdu 5154) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5154 题目意思:有 n 门 processes(编号依次为1,2,...,n),然后给出 m 种关系: ...

  2. 2道acm编程题(2014):1.编写一个浏览器输入输出(hdu acm1088);2.encoding(hdu1020)

    //1088(参考博客:http://blog.csdn.net/libin56842/article/details/8950688)//1.编写一个浏览器输入输出(hdu acm1088)://思 ...

  3. 树形dp(B - Computer HDU - 2196 )

    题目链接:https://cn.vjudge.net/contest/277955#problem/B 题目大意:首先输入n代表有n个电脑,然后再输入n-1行,每一行输入两个数,t1,t2.代表第(i ...

  4. Bestcoder13 1003.Find Sequence(hdu 5064) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5064 题目意思:给出n个数:a1, a2, ..., an,然后需要从中找出一个最长的序列 b1, b ...

  5. 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 ...

  6. (多线程dp)Matrix (hdu 2686)

    http://acm.hdu.edu.cn/showproblem.php?pid=2686     Problem Description Yifenfei very like play a num ...

  7. War Chess (hdu 3345)

    http://acm.hdu.edu.cn/showproblem.php?pid=3345 Problem Description War chess is hh's favorite game:I ...

  8. 2012年长春网络赛(hdu命题)

    为迎接9月14号hdu命题的长春网络赛 ACM弱校的弱菜,苦逼的在机房(感谢有你)呻吟几声: 1.对于本次网络赛,本校一共6名正式队员,训练靠的是完全的自主学习意识 2.对于网络赛的群殴模式,想竞争现 ...

  9. 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 ( ...

随机推荐

  1. linux 安装mysql5.6 yum

    安装mysql: 查看mysql: rpm -qa | grep -i mysql 安装必要的环境 yum -y install gcc gcc-c++ ncurses-devel perl 查看环境 ...

  2. [CodeForces950C]Zebras

    Description 题目地址: Codeforces 题意:给你一串只含01的字符串,判断能否将字符串分为k个子序列,使得子序列满足以下条件: 开头和结尾都是0 相邻的2个数是01或者10 如0, ...

  3. python-9-IO编程

    1-文件读写 f = open('d:/file.txt','r') #如果文件不存在会报异常 print(f.read()) #一次性读取所有内容 f.close() 1.2 由于文件操作会用异常, ...

  4. Oozie wordcount实战

    一.定义 基本概念 Action: An execution/computation task (Map-Reduce job, Pig job, a shell command). It can a ...

  5. Nginx 高级配置

    nginx官方网站:http://nginx.org/ 1.  Nginx连接后端的方式:反向代理(proxy_pass).直连fastcgi(fastcgi_pass) 例子: fastcgi_pa ...

  6. Python 装饰器执行顺序迷思

    Table of Contents 1. 探究多个装饰器执行顺序 1.1. 疑问 1.2. 函数和函数调用的区别 1.3. 装饰器函数在被装饰函数定义好后立即执行 1.4. 疑问的解释 2. 参考资料 ...

  7. Android Kotlin 连接 http

    由于近期网上搜索了很多Android连接到http的方法, 可是2013年以前的方法现在都不能用了,要么报错,要么被遗弃,岁月留下来的东西只能自己整理了. 其实很简单,就一个HttpUtil通用类.可 ...

  8. ionic2升级到ionic3并打包APK

    通过IONIC2升级到3的时候,经过我一系列的测试,以及网上各种办法,现将新测有效的方法记录如下,本人按如下方法,对多个项目升级后,都能正常打包成APK IONIC 2到3的升级: 1.拷贝ionic ...

  9. Pascal小游戏 贪吃蛇

    一段未完成的Pascal贪吃蛇 说这段代码未完成其实是没有源代码格式化,FP中一行最多只有255字符宽. uses crt; const screenwidth=50; screenheight=24 ...

  10. Nodejs的那些事

    前言: Node.js实际上是算是个前端开发,但是我们要做APP自动化涉及到 node.js ,也需要它来帮我们安装一些 packing 一.Node.js安装 1.安装Node.js:立即下载 2. ...