您的位置:首页 > 其它

hdu2196 Computer(树形dp)

2018-01-18 15:12 597 查看

Computer

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


[align=left]Problem Description[/align]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. 
[align=left]Input[/align]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. 
[align=left]Output[/align]For each case output N lines. i-th line must contain number Si for i-th computer (1<=i<=N). 
[align=left]Sample Input[/align]
5
1 1
2 1
3 1
1 1 
Samp
f345
le Output
3
2
3
4
4
题意:给出一棵树,求每个点到树的边缘的最长距离
分析:对于每个点,它到树边缘的最长距离由两部分组成1:它子树中的最长链2:最长逆链(即通过父节点到达树的边缘走的最长链+它到父节点的权值)其实就是分往上走和往下走两种情况对于1很好求,关键是2
对于最长逆链,可以通过父节点的最长子链获得,但是会有这样一种情况,父亲节点的最长链经过该儿子,此时求得的最长链不满足题意,我们要求的是不经过该点的最长逆链
此时就需要记录一下次大子链,通过比较    儿子的最大子链+它到父节点的权值==父节点的最大子链 判断父节点的最大子链是否经过该子节点设dp[i][0]表示该节点的最长子链,dp[i][1]次长子链,dp[i][2]最长逆链
先用一个dfs求出dp[i][0]和dp[i][1]然后再dfs求出dp[i][2]
下面主要给出dp[i][2]的转移方程满足 红字标明条件 dp[son][2]=max(dp[fa][2],dp[fa][1])+e[i].w不满足                      dp[son][2]=max(dp[fa][2],dp[fa][0])+e[i].w
此时会出现这种情况:一个父节点有两个相同长度的最大子链,而根据这个判断,父节点的最大子链经过其中的一条子链(具体不知道那一条),这时应该怎么处理?其实不用考虑这种情况,因为在这种情况下,父节点的次大子链和最大子链相等,刷新最大值的时候无影响
#include<stdio.h>
#include<algorithm>
#include<string.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int N=10005;
int dp
[4],first
,tot;
struct node
{
int v,w,next;
}e
;
void add(int u,int v,int w)
{
e[tot].v=v;
e[tot].w=w;
e[tot].next=first[u];
first[u]=tot++;
}
void dfs(int u,int fa)
{
for(int i=first[u];~i;i=e[i].next)
{
int v=e[i].v;
if(v==fa)continue;
dfs(v,u);
if(dp[u][0]<dp[v][0]+e[i].w)
{
dp[u][1]=dp[u][0];
dp[u][0]=dp[v][0]+e[i].w;
}
else if(dp[u][1]<dp[v][0]+e[i].w)//这里别忘了加,因为可能出现两条长度相同的子链
dp[u][1]=dp[v][0]+e[i].w;

}
}
void dfs2(int u,int fa)
{
for(int i=first[u];~i;i=e[i].next)
{
int v=e[i].v;
if(v==fa)continue;

if(dp[u][0]==dp[v][0]+e[i].w)
dp[v][2]=max(dp[u][2],dp[u][1])+e[i].w;
else
dp[v][2]=max(dp[u][2],dp[u][0])+e[i].w;

dfs2(v,u);
}
}
int main()
{
int n,v,w;
while(~scanf("%d",&n))
{
tot=0;
mem(first,-1);
mem(dp,0);
for(int i=2;i<=n;i++)
{
scanf("%d%d",&v,&w);
add(v,i,w);
}
dfs(1,-1);
dfs2(1,-1);
for(int i=1;i<=n;i++)
printf("%d\n",max(dp[i][0],dp[i][2]));
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: