Computer

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 35047    Accepted Submission(s):
5633

Problem Description
A school bought the first computer some time ago(so
this computer's id is 1). During the recent years the school bought N-1 new
computers. Each new computer was connected to one of settled earlier. Managers
of school are anxious about slow functioning of the net and want to know the
maximum distance Si for which i-th computer needs to send signal (i.e. length of
cable to the most distant computer). You need to provide this information.
Hint: the example
input is corresponding to this graph. And from the graph, you can see that the
computer 4 is farthest one from 1, so S1 = 3. Computer 4 and 5 are the farthest
ones from 2, so S2 = 2. Computer 5 is the farthest one from 3, so S3 = 3. we
also get S4 = 4, S5 = 4. 

Input

Input file contains multiple test cases.In each case
there is natural number N (N<=10000) in the first line, followed by (N-1)
lines with descriptions of computers. i-th line contains two natural numbers -
number of computer, to which i-th computer is connected and length of cable used
for connection. Total length of cable does not exceed 10^9. Numbers in lines of
input are separated by a space.

Output
For each case output N lines. i-th line must contain
number Si for i-th computer (1<=i<=N).
Sample Input
5
1 1
2 1
3 1
1 1
Sample Output
3
2
3
4
4
题意:给你一棵树,求每个节点的最远对点的距离。
题目看似有点难度,但我们想一想两遍dfs求树的直径的过程,第一遍dfs找的是随意一个点的最远对点,它一定是树的直径的端点,那么我们就可以猜想一下是不是每个点的最远对点都是树的直径的端点呢?答案是肯定的。那么我们就可以先把树的直径找出来然后分别求两个端点到所有的点的距离,取最大值即可。
代码如下:
 #include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<algorithm>
#define maxn 10005
using namespace std; struct edge
{
int next;
int to;
int dis;
}g[maxn<<]; inline int read()
{
char c=getchar();
int res=,x=;
while(c<''||c>'')
{
if(c=='-')
x=-;
c=getchar();
}
while(c>=''&&c<='')
{
res=res*+(c-'');
c=getchar();
}
return x*res;
} int n,aa,bb,num,root,ans;
int last[maxn],d[maxn],dp[maxn],dp1[maxn]; inline void add(int from,int to,int dis)
{
g[++num].next=last[from];
g[num].to=to;
g[num].dis=dis;
last[from]=num;
} void dfs(int x)
{
d[x]=;
for(int i=last[x];i;i=g[i].next)
{
int v=g[i].to;
if(!d[v])
{
dp[v]=dp[x]+g[i].dis;
dfs(v);
}
}
} void dfs1(int x)
{
d[x]=;
for(int i=last[x];i;i=g[i].next)
{
int v=g[i].to;
if(!d[v])
{
dp[v]=dp[x]+g[i].dis;
dfs1(v);
}
}
} void dfs2(int x)
{
d[x]=;
for(int i=last[x];i;i=g[i].next)
{
int v=g[i].to;
if(!d[v])
{
dp1[v]=dp1[x]+g[i].dis;
dfs2(v);
}
}
} int main()
{
while(scanf("%d",&n)!=EOF)
{
ans=;num=;
memset(last,,sizeof(last));
memset(dp,,sizeof(dp));
memset(d,,sizeof(d));
memset(dp1,,sizeof(dp1));
for(int i=;i<=n;i++)
{
aa=read();bb=read();
add(i,aa,bb);
add(aa,i,bb);
}
dfs();
for(int i=;i<=n;i++)
{
if(dp[i]>ans)
{
ans=dp[i];
root=i;
}
}
memset(dp,,sizeof(dp));
memset(d,,sizeof(d));
dfs1(root);
ans=;
memset(d,,sizeof(d));
for(int i=;i<=n;i++)
{
if(dp[i]>ans)
{
ans=dp[i];
root=i;
}
}
dfs2(root);
for(int i=;i<=n;i++)
{
printf("%d\n",max(dp[i],dp1[i]));
}
}
return ;
}

Don't waste your time on a man/woman, who isn't willing to waste their time on you.
不要为那些不愿在你身上花费时间的人而浪费你的时间

--snowy

2019-01-18  07:49:19

HDU 2196 Compute --树形dp的更多相关文章

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

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

  2. HDU 2196 Computer 树形DP经典题

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

  3. HDU 2196 Computer 树形DP 经典题

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

  4. HDU - 2196(树形DP)

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

  5. hdu 2196【树形dp】

    http://acm.hdu.edu.cn/showproblem.php?pid=2196 题意:找出树中每个节点到其它点的最远距离. 题解: 首先这是一棵树,对于节点v来说,它到达其它点的最远距离 ...

  6. hdu 2196 Computer(树形DP)

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

  7. hdu 2196 Computer 树形dp模板题

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

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

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

  9. HDU 2196 Computer (树dp)

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

随机推荐

  1. react的jsx语法

    在webpack.config.js中配置解析的loader { test:/\.jsx?$/, use:{ loader:"babel-loader", options:{ pr ...

  2. RecyclerView的点击、滑动、拖动事件

    效果图: 在gradle里导包   implementation 'com.android.support:recyclerview-v7:28.0.0' activity_main <?xml ...

  3. 简单实现计算机上多个jdk环境切换

    实现多个jdk环境切换,大致有两种方式 安装两个jdk,并配置相应的环境变量,在java的控制面板中修改设置 非主要的jdk仅仅是用来测试,并不常用,故只要让ide配置对应的jdk位置就可以了,属于懒 ...

  4. 配置启动MySQL的Docker容器

    docker run -d -p : --name mysql -e MYSQL_ROOT_PASSWORD= mysql:

  5. centos7.4 linux 指令

    1.查看版本 lsb_release -a 2.查看mysql路径 whereis mysql 3.查看编码 locale 修改语言编码 经过在网上查找资料发现,Centos 7已经不采用/etc/s ...

  6. Android查看联系人简单记录

    简单实现打印联系人信息,可以作为插入联系人的基础和主要代码块,作为个人记录的小逻辑 package com.lgqrlchinese.contactstest; import android.Mani ...

  7. pc安装完成charles成功,小米安装crt证书失败

    问题描述: 今天在学习爬虫爬取APP内容时,需要安装crt证书.根据静谧大大的书,前面都挺顺利的.但在我的小米手机上安装crt证书时,出现了错误.手机显示无法安装. 解决之道: 1.不要用小米手机自带 ...

  8. Gym - 101350A Sherlock Bones(思维)

    The great dog detective Sherlock Bones is on the verge of a new discovery. But for this problem, he ...

  9. 微服务之服务中心—zookeeper

    微服务中的服务注册与发现 传统的项目中,某个服务访问另一个服务,可以通过在配置文件中记录其他服务静态地址的形式进行访问,通常这个配置文件也很少更新,模式如下图: 而在微服务中,每个功能可能都是一个独立 ...

  10. Docker:私有仓库registry [十一]

    一.运行docker私有仓库 安装registry docker run -d -p 5000:5000 --restart=always --name registry -v /opt/myregi ...