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

/*
树形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防火墙的管理和策略控制

    iptables 一:IPtables防火墙的简介 IPTABLES 是与最新的 3.5 版本 Linux 内核集成的 IP 信息包过滤系统.如果 Linux 系统连接到因特网或 LAN.服务器或连接 ...

  2. 柱状图多系列php动态实现(ec)

    <?php require_once 'data.php'; $arr1=$a->sum('answer','ask_id=1'); $arr2=$a->sum('answer',' ...

  3. C指针(1)——指针在数组中的应用(程序讲解)

    2-1.c数组指针的定义: #include <stdio.h> int main() { char str[]="China Beijing Fujian"; //定 ...

  4. PLY手册翻译

    https://www.kancloud.cn/kancloud/ply/42143 http://wiki.jikexueyuan.com/project/python-lex-yacc/ply-0 ...

  5. python学习之map函数和reduce函数的运用

    MapReduce:面向大型集群的简化数据处理引文 map()函数 Python中的map()函数接收两个参数,一个是调用函数对象(python中处处皆对象,函数未实例前也可以当对象一样调用),另一个 ...

  6. Makefile (3) 基本语法和使用

    make是用来管理一个工程项目的工具 . Makefile就是这个项目文件 . 1.Makefile 是由若干条规则组成的,每个规则的语法如下所示 : #规则 targets: prerequisit ...

  7. realloc函数的用法

    realloc(void *__ptr, size_t __size):更改已经配置的内存空间,即更改由malloc()函数分配的内存空间的大小. 如果将分配的内存减少,realloc仅仅是改变索引的 ...

  8. Snowflake Snow Snowflakes【Poj3349】

    Description You may have heard that no two snowflakes are alike. Your task is to write a program to ...

  9. Spark&Hive结合起来

    1.spark与Hive结合起来 前提:当你spark的版本是1.6.1的时候,你的Hive版本要1.2.1,用别的版本会有问题 我们在做的时候,Hive的版本很简单,我们只需要解压缩,告诉他Hive ...

  10. url_maneger.py

    coding=UTF-8 # url管理器 class urlManeger: def __init__(self): self.new_urls = set() self.old_urls = se ...