首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【HDU】2196 - Computer(树的直径)

【HDU】2196 - Computer(树的直径)

作者头像
FishWang
发布2025-08-27 09:46:09
发布2025-08-27 09:46:09
13100
代码可运行
举报
运行总次数:0
代码可运行

点击打开题目

Computer

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

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

代码语言:javascript
代码运行次数:0
运行
复制
   5
1 1
2 1
3 1
1 1

Sample Output

代码语言:javascript
代码运行次数:0
运行
复制
   3
2
3
4
4

Author

scnu

看网上大多数解法都是用树形dp做的,看了半天还是有点不清晰。

然后就发现了直接用dfs的方法做的,感觉想法很好。

第一次dfs,从任意一个点找树的直径,得到最长链的起点。

第二次dfs,从起点开始,得到链的终点和每一个点距起点的距离。

第三次dfs,从终点开始,得到每一个点距终点的距离。

最后的结果是这个点到起点和到终点距离的最大值。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define MAX 10000
vector<int> mapp[MAX+11];
vector<int> dis[MAX+11];
bool vis[MAX+11];
int f[2][MAX+11];
int ans[MAX+11];
int n;
void init()
{
	for (int i = 1 ; i <= n ; i++)
	{
		mapp[i].clear();
		dis[i].clear();
	}
}
int dfs(int x,int p)
{
	int maxx = 0 , pos = x;
	queue<int> q;
	CLR(vis,0);
	f[p][x] = 0;
	q.push(x);
	vis[x] = true;
	while (!q.empty())
	{
		int t = q.front();
		q.pop();
		for (int i = 0 ; i < mapp[t].size() ; i++)
		{
			if (!vis[mapp[t][i]])
			{
				int ne = mapp[t][i];
				q.push(ne);
				f[p][ne] = f[p][t] + dis[t][i];
				vis[ne] = true;
				if (f[p][ne] > maxx)
				{
					maxx = f[p][ne];
					pos = ne;
				}
			}
		}
	}
	return pos;
}
int main()
{
	while (~scanf ("%d",&n))
	{
		init();
		for (int i = 2 ; i <= n ; i++)
		{
			int t1,t2;
			scanf ("%d %d",&t1,&t2);
			mapp[i].push_back(t1);
			mapp[t1].push_back(i);
			dis[i].push_back(t2);
			dis[t1].push_back(t2);
		}
		CLR(f,0);
		int t1 = dfs(1,0);		//找出起点 
		int t2 = dfs(t1,1);		//求所有点到起点的距离
		CLR(f[0],0);
		dfs(t2,0); 		//求所有点到终点的距离 
		for (int i = 1 ; i <= n ; i++)
			printf ("%d\n",max(f[0][i] , f[1][i]));
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Computer
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档