hdu2196 Computer 两次dfs

Computer

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

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
 

Statistic |  Submit |  Discuss |  Note

这个题的意思是让你求出一个结点到其他结点的最远距离,要两次搜索,第一次搜索找出结点到其他节点(这里仅指子节点)的最长距离(以下简称最长距离)和第二长距离,第二次是为了找出所有节点到其他节点的最远距离(从父亲节点开始往下算出真正的最远距离)。a结点最远距离是由a-b-c-d-e这几个结点组成,当你求b的最远距离时你可能会把dis[a][b](a和b之间的距离)+longest[a](a的最远距离)当作了b的最远距离,这明显是错的,所以要求第一和第二长的距离。我当时做这个题的时候不太懂解题报告说的什么意思,看了几遍才明白,做这个的要是不明白解题报告什么意思最好是耐着性子把解题报告多看几遍,看多了自然就懂了
Memory: 720 KBTime: 31 MSLanguage: C++Result: Accepted
#include<stdio.h>
#include<vector>
#include<iostream>
using namespace std;
#define N 10010

struct node{
    int to,cap;//to是当前的值,cap是与父亲节点的权值
}a;

vector<node>vt[10010];

int n,dp[N],downf[N],downs[N],vis[N];

int max(int &i,int &j)
{
	return i>j?i:j;
}

void dfs1(int x)//从上往下,递归
{
	int len,i,j,max=-1,flag1=-1,flag2=-1;
	len=vt[x].size();
	if(len==0)
		return ;
	if(downf[x])//如果已经搜过就返回
		return ;
	for(i=0;i<len;i++)
	{
		j=vt[x][i].to;//子节点
		dfs1(j);//先dfs再处理
		if(max<downf[j]+vt[x][i].cap)
		{
			max=downf[j]+vt[x][i].cap;//j到到其子树最远距离加上j和i之间的距离
			flag1=i;//记录最远路径(子节点在x的序号)
		}
	}
	//printf(" %d~%d ",x,max);
	vis[x]=flag1;//记录最远边,即vt[x][vis[x]].to
	downf[x]=max;
	max=-1;
	for(i=0;i<len;i++)
	{
		j=vt[x][i].to;
		if(i!=flag1&&max<downf[j]+vt[x][i].cap)//保证不与最长距离重边
		{
			max=downf[j]+vt[x][i].cap;
			flag2=i;//当前节点的子节点在x的序号
		}
	}
	if(flag2!=-1)
		downs[x]=max;//第二长
}

void dfs2(int x)//从上往下
{
	int len=vt[x].size(),i,j;
	if(len==0)
		return ;
	for(i=0;i<len;i++)
	{//先处理后dfs,因为父节点到其子树的最远距离已知。原理是子节点到父节点的距离+父节点最远(或第二)距离=子节点最远距离
		j=vt[x][i].to;
		if(i==vis[x])//j在父亲节点的最长路径上
			dp[j]=max(downs[x],dp[x])+vt[x][i].cap;//选择第二长路径
		else dp[j]=max(downf[x],dp[x])+vt[x][i].cap;//j不在最长路径上,选第一长路径
		dfs2(j);
	}
}

int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		int i,j,k;
		for(i=0;i<n;i++)
			vt[i].clear();
		for(i=2;i<=n;i++)
		{
			scanf("%d%d",&j,&a.cap);
			a.to=i;
			vt[j].push_back(a);//在父节点后记录子节点
		}
		memset(dp,0,sizeof(dp));
        memset(downf,0,sizeof(downf));
        memset(downs,0,sizeof(downs));
		dfs1(1);
		dfs2(1);
		for(i=1;i<=n;i++)
			printf("%d\n",max(downf[i],dp[i]));
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值