HDU 2196 Computer 树形dp
来源:互联网 发布:tcl网络电视如何看直播 编辑:程序博客网 时间:2024/06/16 04:39
Computer
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 31200 Accepted Submission(s): 3988
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
Author
scnu
Recommend
lcy | We have carefully selected several similar problems for you: 1561 1011 3456 2242 2602
题意:
给出一棵树,输出树上每个点到其他点的最长距离。
题解:
hdu终于可以登啦撒花~【明明之前是你自己傻一直没找到正确的打开方式
对于每个点,它的最长距离只会从两个方向转移来,要么1.从它的儿子来,要么2.它的父亲来。
于是我们可以先dfs一遍,找到每个点从它的儿子转移来的最长路径和次长路径。【次长路径等求从父亲转移来的时候会用到。
然后再进行一遍dfs,求出每个点从父节点转移来的最长路径。注意,由于父节点的最长路径可能是从该儿子转移来的,所以此时就不能用通过它自己的路径去更新这个答案,而是要用到达父节点的次长路。
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 10000 + 10;inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();} return x*f;}int n;struct node{ int pre,v,w;}e[N<<1];int num=0,head[N];void addedge(int from,int to,int w){ e[++num].pre=head[from],e[num].v=to; e[num].w=w,head[from]=num;}int dis[N][2],son[N];//dis[i][0]表示i这个节点离它的子树的叶子节点的最大距离//dis[i][1]表示次大距离 void dfs1(int u,int f){ dis[u][0]=dis[u][1]=0,son[u]=0; for(int i=head[u];i;i=e[i].pre){ int v=e[i].v; if(v==f) continue; dfs1(v,u); if(dis[v][0]+e[i].w>dis[u][0]){ dis[u][1]=dis[u][0]; dis[u][0]=dis[v][0]+e[i].w; son[u]=v; } else if(dis[v][0]+e[i].w>dis[u][1]) dis[u][1]=dis[v][0]+e[i].w; }}int dist[N];void dfs2(int u,int f){ for(int i=head[u];i;i=e[i].pre){ int v=e[i].v; if(v==f) continue; if(v==son[u]) dist[v]=max(dis[u][1],dist[u])+e[i].w; else dist[v]=max(dis[u][0],dist[u])+e[i].w; dfs2(v,u); }}#define ms(x,y) memset(x,y,sizeof(x))void update(){ ms(head,0);num=0;ms(dist,0);}int main(){ while(~scanf("%d",&n)){ update(); for(int i=2;i<=n;++i){ int v=read(),w=read(); addedge(i,v,w);addedge(v,i,w); } dfs1(1,0);//求出一个点到子树的最大值 dfs2(1,0); for(int i=1;i<=n;++i) printf("%d\n",max(dist[i],dis[i][0])); } return 0;}
- HDU 2196 Computer(树形dp)
- 【树形DP】 HDU 2196 Computer
- HDU 2196 Computer(树形DP)
- Hdu 2196 Computer(树形dp)
- Computer - HDU 2196 树形dp
- Hdu 2196 Computer (树形dp)
- HDU 2196 Computer | 树形dp
- 【树形DP】 HDU 2196 Computer
- HDU 2196 Computer (树形DP)
- hdu 2196 Computer (树形dp)
- *HDU 2196 - Computer(树形DP)
- [HDU 2196] Computer 树形dp
- HDU 2196Computer 树形dp
- 【hdu】2196 Computer【树形dp】
- HDU 2196 Computer [树形dp]
- hdu 2196 Computer(树形DP)
- 树形DP-HDU-2196-Computer
- 【HDU】2196 Computer 树形dp
- ElasticSearch基础
- BZOJ1827 [Usaco2010 Mar]gather 奶牛大集会
- Uiautomator的基础对象
- Android图片加载库的封装实战
- 数据结构实验之链表一:顺序建立链表
- HDU 2196 Computer 树形dp
- JAVA 异常
- RxJava + httpURLConnectionde 的简单测试demo,可以拿来处理一般的android访问网络的线程问题
- 微信支付
- 使用Angular4+kendo ui for Angular+ webapi+sqlserver创建一个简单的省市选择程序(二)
- void及void指针类型整理
- 内联函数、普通函数、宏定义
- Grade重复依赖解决方案-compileOnly
- Java导出2003、2007版Excel