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

时间:2024-01-16 16:20:02

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.
hdoj 2196 Computer【树的直径求所有的以任意节点为起点的一个最长路径】

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;
}