hdoj 2196 Computer【树的直径求所有的以任意节点为起点的一个最长路径】

Computer

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

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

刚开始用暴力,对每一个节点都bfs一次不出所料的LTE

题意:购置电脑,每新买一台电脑就要把它连接到购置的上一台电脑上  求所有的以任意节点为起点的一个最长路径,

所给测试数据的意思是 :两个数a,b表示a电脑与第i+1台电脑相连,所需要的电缆的长度为b(i指的是第i行)

如测试数据中的2 1表示2与3相连权值为1。  3 1表示电脑3与电脑4相连权值为1;

题解:用三次bfs找到最长路的两个端点u和v因为题中说每台新买的电脑都连接到上一台电脑上,所以每台电脑的

连接都没有分叉,即节点的最长路要么是到端点u的距离要么是到端点v的距离(即取较大的那个),先用两次

bfs找到一个端点记录下每个点到这个端点的距离,再用一次bfs找到另一个端点,再记录下每个点到这个端点的距离

最后遍历每个点求出最长路

#include<stdio.h>
#include<string.h>
#include<queue>
#define MAX 40100
#define maxn(x,y)(x>y?x:y)
using namespace std;
int head[MAX];
int vis[MAX],dis[MAX];
int n,m,ans,ant;
int sum,beg,en;
int a[MAX],b[MAX];
struct node
{
	int u,v,w;
	int next;
}edge[MAX];
void add(int u,int v,int w)
{
	edge[ans].u=u;
	edge[ans].v=v;
	edge[ans].w=w;
	edge[ans].next=head[u];
	head[u]=ans++;
}
void getmap()
{
	int i,j,a,b;
	ans=0;
	memset(head,-1,sizeof(head));
	for(i=2;i<=n;i++)
	{
		scanf("%d%d",&a,&b);
		add(a,i,b);
		add(i,a,b);
	}
}
void bfs(int sx)
{
	int i,j;
	queue<int>q;
	sum=0;
	memset(vis,0,sizeof(vis));
	memset(dis,0,sizeof(dis));
	vis[sx]=1;
	beg=sx;
	q.push(sx);
	while(!q.empty())
	{
		int top=q.front();
		q.pop();
		for(i=head[top];i!=-1;i=edge[i].next)
		{
			int k=edge[i].v;
			if(!vis[k])
			{
				vis[k]=1;
				dis[k]=dis[top]+edge[i].w;
				q.push(k);
			}
			if(sum<dis[k])
			{
				sum=dis[k];
				beg=k;
			}
		}
	}
}
void solve()
{
	int i,j;
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	bfs(1);
	bfs(beg);
	en=beg;//找到第一个端点
	for(i=1;i<=n;i++)
		a[i]=dis[i];//记录每个点到这个端点的距离
	bfs(en);//找到另一个端点
	for(i=1;i<=n;i++)
        b[i]=dis[i];//记录每个点到这个端点的距离
    for(i=1;i<=n;i++)
    {
    	ant=0;
    	ant=maxn(a[i],b[i]);
    	printf("%d\n",ant);
	}
}
int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		getmap();
		solve();
	}
	return 0;
}

  

时间: 2024-08-09 02:20:53

hdoj 2196 Computer【树的直径求所有的以任意节点为起点的一个最长路径】的相关文章

HDOJ 2196 Computer 树的直径

由树的直径定义可得,树上任意一点到树的直径上的两个端点之一的距离是最长的... 三遍BFS求树的直径并预处理距离....... Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3522    Accepted Submission(s): 1784 Problem Description A school bought

hdu 2196 Computer 树的直径

Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) 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

hdu2196 Computer(树的直径||树中的最长路径)

Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5232    Accepted Submission(s): 2640 Problem Description A school bought the first computer some time ago(so this computer's id is 1). D

在二进制树中的节点之间的最大距离(最长路径树)——递归解决方案

上一篇文章即是对这一主题的变化.并给出了一个非递归溶液. 我给出原题的一种递归解法. 将会看到,现比較上篇博文.今天给出的递归解法的代码实现是相当简洁的. 问题描写叙述: 假设我们把二叉树看成一个图.父子节点之间的连线看成是双向的,我们姑且定义"距离"为两节点之间边的个数. 写一个程序.求一棵二叉树中相距最远的两个节点之间的距离.測试用的树: n1 /             \ n2             n3 /        \ n4          n5 /     \  

hdu 2196 computer 树状dp

Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3731    Accepted Submission(s): 1886 Problem Description A school bought the first computer some time ago(so this computer's id is 1). Du

HDU 2196 Computer(树形DP求最长路)

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 a

hdu4612 无向图中任意添加一条边后使桥的数量最少 / 无向图缩点+求树的直径

题意如上,含有重边(重边的话,俩个点就可以构成了边双连通). 先缩点成树,在求数的直径,最远的连起来,剩下边(桥)的自然最少.这里学习了树的直径求法:第一次选任意起点U,进行bfs,到达最远的一个点v(level最深)该点必然是树的直径的一个端点,,再从该点出发,bfs,到最深的一点,该点深度就是直径.(证明:先假设u,是直径上一点,S,T是直径的端点,设v!=t,则有(V,U)+(U,S)>(T,U)+(U,S),矛盾,故t=v:若u不是直径上一点,设u到直径上的一点为x,同理易证. 最后 缩

HDU 4607 Park visit (求树的直径)

解题思路: 通过两次DFS求树的直径,第一次以任意点作为起点,找到距离该点距离最远的点,则可以证明这个点一定在树的直径上,然后以该点为起点进行DFS得到的最长路就是树的直径. 最后的询问,如果K <= D + 1则可以沿着直径走,距离为K  -  1, 如果K >= D + 1,则需要走直径旁边的分支,每访问一个点距离为2(从直径到这个点,再返回到直径上). #include <iostream> #include <cstring> #include <cstd

ural 1145 Rope in the Labyrinth 图中 bfs求树的直径

1145. Rope in the Labyrinth Time limit: 0.5 second Memory limit: 64 MB A labyrinth with rectangular form and size m × n is divided into square cells with sides' length 1 by lines that are parallel with the labyrinth's sides. Each cell of the grid is