链接:http://acm.hdu.edu.cn/showproblem.php?

pid=2196

题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问每台电脑和其它电脑的最远距离是多少。

思路:这是一道树形DP的经典题目。须要两次DFS,第一次DFS找到树上全部的节点在不同子树中的最远距离和次远的距离(在递归中进行动态规划就可以),第二次DFS从根向下更新出终于答案。对于每次更新到的节点u,他的最远距离可能是来自u的子树,或者是u的父亲节点的最远距离。假设u的父亲节点的最远距离是在第一次DFS过程中更新自u的话,那么u的最远距离就不能更新自u的父亲节点的最远节点,而是有可能更新自u的父亲节点的次远距离,这就是每次更新时要记录节点的次远距离的原因。

代码:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define eps 1e-8
#define INF 0x7fffffff
#define maxn 10005
#define PI acos(-1.0)
#define seed 31//131,1313
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
int dp[maxn][2],from[maxn],head[maxn],top;
void init()
{
memset(head,-1,sizeof(head));
memset(dp,0,sizeof(dp));
top=0;
}
struct Edge
{
int v,w;
int next;
} edge[maxn*2];
void add_edge(int u,int v,int w)
{
edge[top].v=v;
edge[top].w=w;
edge[top].next=head[u];
head[u]=top++;
}
void dfs_first(int u,int f)
{
from[u]=u;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v,w=edge[i].w;
if(v==f)
continue;
dfs_first(v,u);
if(dp[v][0]+w>dp[u][0])
{
from[u]=v;
dp[u][1]=dp[u][0];
dp[u][0]=dp[v][0]+w;
}
else if(dp[v][0]+w>dp[u][1])
dp[u][1]=dp[v][0]+w;
}
}
void dfs_second(int u,int f,int k)
{
if(u!=f)
if(from[f]!=u)
{
if(dp[f][0]+k>dp[u][0])
{
from[u]=f;
dp[u][1]=dp[u][0];
dp[u][0]=dp[f][0]+k;
}
else if(dp[f][0]+k>dp[u][1])
dp[u][1]=dp[f][0]+k;
}
else
{
if(dp[f][1]+k>dp[u][0])
{
from[u]=f;
dp[u][1]=dp[u][0];
dp[u][0]=dp[f][1]+k;
}
else if(dp[f][1]+k>dp[u][1])
dp[u][1]=dp[f][1]+k;
}
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v,w=edge[i].w;
if(v==f)
continue;
dfs_second(v,u,w);
}
}
int main()
{
int T,v,w;
while(~scanf("%d",&T))
{
init();
for(int i=2; i<=T; i++)
{
scanf("%d%d",&v,&w);
add_edge(v,i,w);
add_edge(i,v,w);
}
dfs_first(1,1);
dfs_second(1,1,0);
for(int i=1;i<=T;i++)
printf("%d\n",dp[i][0]);
}
return 0;
}

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

  1. HDU 2196 Computer 树形DP 经典题

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

  2. hdu 2196 Computer 树形dp模板题

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

  3. hdu 2196 Computer(树形DP经典)

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

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

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

  5. hdu 2196 Computer(树形DP)

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

  6. 51nod 1353 树 | 树形DP经典题!

    51nod 1353 树 | 树形DP好题! 题面 切断一棵树的任意条边,这棵树会变成一棵森林. 现要求森林中每棵树的节点个数不小于k,求有多少种切法. 数据范围:\(n \le 2000\). 题解 ...

  7. POJ 1155 TELE 背包型树形DP 经典题

    由电视台,中转站,和用户的电视组成的体系刚好是一棵树 n个节点,编号分别为1~n,1是电视台中心,2~n-m是中转站,n-m+1~n是用户,1为root 现在节点1准备转播一场比赛,已知从一个节点传送 ...

  8. HDU 2196 Computer (树dp)

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

  9. HDU - 2196(树形DP)

    题目: A school bought the first computer some time ago(so this computer's id is 1). During the recent ...

随机推荐

  1. ES 断路器——本质上保护OOM提前抛出异常而已

    监控fielddata使用了多少内存以及是否有数据被驱逐是非常重要的.大量的数据被驱逐会导致严重的资源问题以及不好的性能. Fielddata使用可以通过下面的方式来监控: 对于单个索引使用 {ref ...

  2. thymeleaf 引入js css 无效

    转自:https://blog.csdn.net/qq_33833327/article/details/81388502

  3. 第20章 Redis配置

    20.1 Redis基础配置文件 20.2 Redis备份(持久化) save 900 1 save 300 10 save 60 10000 # By default Redis will stop ...

  4. 虚拟机CentOS设置IP

    虚拟机里Centos7的IP地址查看方法 本地虚拟机安装了CentOS 7,想通过ftp上传文件,发现通过ifconfig,没有inet这个属性 查看ens33网卡的配置:vi /etc/syscon ...

  5. 【Linux】YUM Repositories for CentOS, RHEL & Fedora Systems

    这里是官方wiki:https://wiki.centos.org/AdditionalResources/Repositories 一.简介 YUM(Yellowdog Updater Modifi ...

  6. unity3d 让物体移动到点击位置

    using UnityEngine; using System.Collections; public class test : MonoBehaviour { //在场景中鼠标点击地面后,角色可以移 ...

  7. hiho一下 第173周

    题目1 : A Game 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi and Little Ho are playing a game. Ther ...

  8. arttemplate.js简洁写法案例

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  9. MySQL 5.6 Reference Manual-14.1 Introduction to InnoDB

    14.1 Introduction to InnoDB 14.1.1 InnoDB as the Default MySQL Storage Engine 14.1.2 Checking InnoDB ...

  10. golang new和make的区别

    自:http://www.cnblogs.com/ghj1976/archive/2013/02/12/2910384.html make用于内建类型(map.slice 和channel)的内存分配 ...