题意自己看(猜)

题解

这题很经典,就是记录dp[i][0/1/2]分别代表,从i点向下最大和次大深度,和向上最大深度。

然后转移就行了。

我的写法可能太丑了。死活调不出来,写了一个漂亮的

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=;
int dp[N][],cnt,head[N],longest[N],n;
struct edge{
int to,nxt,w;
}e[N*];
void add(int u,int v,int w){
cnt++;
e[cnt].nxt=head[u];
e[cnt].to=v;
e[cnt].w=w;
head[u]=cnt;
}
void dfs1(int u,int fa){
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to;
if(v==fa)continue;
dfs1(v,u);
if(dp[v][]+e[i].w>dp[u][]){
longest[u]=v;
dp[u][]=dp[u][];
dp[u][]=dp[v][]+e[i].w;
}
else if(dp[v][]+e[i].w>dp[u][]){
dp[u][]=dp[v][]+e[i].w;
}
}
}
void dfs2(int u,int fa){
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to;
if(v==fa)continue;
if(v==longest[u])dp[v][]=max(dp[u][],dp[u][])+e[i].w;
else dp[v][]=max(dp[u][],dp[u][])+e[i].w;
dfs2(v,u);
}
}
int main(){
while(scanf("%d",&n)!=EOF){
memset(head,,sizeof(head));
cnt=;
memset(dp,,sizeof(dp));
memset(longest,,sizeof(longest));
for(int i=;i<=n;i++){
int f,w;
scanf("%d%d",&f,&w);
add(f,i,w);add(i,f,w);
}
dfs1(,);
dfs2(,);
for(int i=;i<=n;i++){
printf("%d\n",max(dp[i][],dp[i][]));
}
}
return ;
}

HDU 2196 Computer(经典树形DP)的更多相关文章

  1. 题解报告:hdu 2196 Computer(树形dp)

    Problem Description A school bought the first computer some time ago(so this computer's id is 1). Du ...

  2. HDU 3534 Tree (经典树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3534 题意: 给你一棵树,问你有多少对点的距离等于树的直径. 思路: dp[i][0]表示在i的子树中 ...

  3. HDU 2196 Computer 树形DP经典题

    链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...

  4. HDU 2196 Computer 树形DP 经典题

    给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权, ...

  5. HDU 2196.Computer 树形dp 树的直径

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. HDU 2136:Computer(树形DP)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 Computer     Description A school bought the fi ...

  7. HDU 2196 Computer (树dp)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 给你n个点,n-1条边,然后给你每条边的权值.输出每个点能对应其他点的最远距离是多少 ...

  8. HDU 1561 The more, The Better 经典树形DP

    The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  9. hdu 1520Anniversary party(简单树形dp)

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. to_string函数(将数字转换成字符串)

    一般常用的转换字符串的方法std::stringstream,但是效率较低;目前C ++11提供了std::to_string 效率方面:C风格的sprintf()没有动态分配内存效率最高;std:: ...

  2. 给iview组件select设置默认值

    1.首先,给select加一个v-model,如: <Select v-model="exam_name" > <Option v-for="(item ...

  3. python dns 服务器

    import socketserver import struct import threading # DNS Query class SinDNSQuery: def __init__(self, ...

  4. Lapack下载安装

    安装 1.下载压缩文件 wget http://www.netlib.org/lapack/lapack-3.8.0.tar.gz 2.解压缩 tar -zxvf lapack-3.8.0.tar.g ...

  5. vue如何根据返回的值对元素进行样式渲染

    1.最终显示样式: 需要:根据任务状态值,显示不同颜色的原点表示任务状态,以及对优先级的数据,进行☆标记 2.代码实现: 在<el-table-column>中需要显示的内容前面,添加图标 ...

  6. STM32 HAL库利用DMA实现串口不定长度接收方法

    参考:https://blog.csdn.net/u014470361/article/details/79206352 我这里使用的芯片是 F1 系列的,主要是利用 DMA 数据传输方式实现的,在配 ...

  7. react中的跨域问题

    react中的跨域问题

  8. HDU 4321 Contest 3

    题意:给定a和b,n,让你求b+a, b+2*a, .......b+n*a里面有多少1. 当统计第K位的时候 可以注意到 第 B+T*A 和 B+(T+2^(K+1))*A 位是相同的 那么 第K位 ...

  9. HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException

    HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException type ...

  10. spring web mvc第一天

    spring  web mvc 感觉就是高大上啊!啥都是配置文件就能够了.所以第一步就是弄清楚配置文件使用和总体框架的流程! Spring web mvc最重要的当然是Controller,也就是首先 ...