HDU-2196 Computer(树的直径+DP)

Computer

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


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
 
  
51 12 13 11 1
 

Sample Output
 
  
32344


一、原题地址

点我传送


二、大致题意

    求树上各点能到达的最远距离。


三、思路

    若将树从直径拎起,将直径的两端点标为n1.n2,那么对于树上的点来讲,这个点所能得到的最大距离l=max(这个点到n1的距离,这个点到n2的距离)。

    1、当我们随意取树上的一个点求他能到达的最远的点,这个最远的点一定是n1或n2中的一个。

    所以我们从1节点入手跑一遍DFS就得到了直径的其中一个端点记为fir。

   2、 此时再从fir入手求他能到达的最远的点,那么这个点一定是另外一个端点,记作sec。

    同时我们也可以得到树中各点到达fir的距离,记录在dp[i]中。

    3、此时的dp[i]并不是答案,因为这只是点到fir的距离,有可能某点到达sec的距离是大于到达fir距离的。

    所以只需要再从sec入手反向的跑一遍然后更新一下dp[i]取一个较大的值就可以啦。


四、代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<list>
using namespace std;
const int INF = 0x3f3f3f3f;
#define LL long long int 
long long  gcd(long long  a, long long  b) { return a == 0 ? b : gcd(b % a, a); }



int N;
int dp[10005];
int fir, sec, maxx;
bool vis[10005];
struct Edge
{
	int v, w;
};
vector<Edge>e[10005];
void dfs_fir(int u, int dis)
{
	if (dis > maxx)
	{
		fir = u;
		maxx = dis;
	}
	for (int i = 0; i < e[u].size(); i++)
	{
		Edge next = e[u][i];
		if (!vis[next.v])
		{
			vis[next.v] = true;
			dfs_fir(next.v, dis + next.w);
			vis[next.v] = false;
		}
	}
}
void dfs_sec(int u, int dis)
{
	dp[u] = dis;
	if (dis > maxx)
	{
		sec = u;
		maxx = dis;
	}
	for (int i = 0; i < e[u].size(); i++)
	{
		Edge next = e[u][i];
		if (!vis[next.v])
		{
			vis[next.v] = true;
			dfs_sec(next.v, dis + next.w);
			vis[next.v] = false;
		}
	}
}
void dfs_ans(int u, int dis)
{
	dp[u] = max(dis, dp[u]);
	for (int i = 0; i < e[u].size(); i++)
	{
		Edge next = e[u][i];
		if (!vis[next.v])
		{
			vis[next.v] = true;
			dfs_ans(next.v, dis + next.w);
			vis[next.v] = false;
		}
	}
}
void work_for_first()
{
	fir = 1;
	maxx = 0;
	memset(vis, false, sizeof(vis));
	vis[1] = true;
	dfs_fir(1, 0);
}
void work_for_second()
{
	sec = 1;
	maxx = 0;
	memset(vis, false, sizeof(vis));
	memset(dp, 0, sizeof(dp));
	vis[fir] = true;
	dfs_sec(fir, 0);
}
void work_for_answer()
{
	memset(vis, false, sizeof(vis));
	vis[sec] = true;
	dfs_ans(sec, 0);
}
int main()
{
	while (~scanf("%d", &N))
	{
		for (int i = 0; i <= N; i++)
			e[i].clear();
		for (int i = 2; i <= N; i++)
		{
			Edge t, tt;
			scanf("%d%d", &t.v, &t.w);
			tt.v = i, tt.w = t.w;
			e[i].push_back(t);
			e[t.v].push_back(tt);
		}
		work_for_first();
		work_for_second();
		work_for_answer();
		for (int i = 1; i <= N; i++)
			printf("%d\n", dp[i]);
	}
	getchar();
	getchar();
}

猜你喜欢

转载自blog.csdn.net/amovement/article/details/80628924