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

  

 
 
 
 
 

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

  1. HDOJ 2196 Computer 树的直径

    由树的直径定义可得,树上随意一点到树的直径上的两个端点之中的一个的距离是最长的... 三遍BFS求树的直径并预处理距离....... Computer Time Limit: 1000/1000 MS ...

  2. hdu 2196 Computer 树的直径

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem ...

  3. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  4. HDOJ --- 2196 Computer

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  5. [hdu2196]Computer树的直径

    题意:求树中距离每个节点的最大距离. 解题关键:两次dfs,第一次从下向上dp求出每个节点子树中距离其的最大距离和不在经过最大距离上的子节点上的次大距离(后序遍历),第二次从上而下dp求出其从父节点过 ...

  6. HDU 2196 Computer (树dp)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 给你n个点,n-1条边,然后给你每条边的权值.输出每个点能对应其他点的最远距离是多少 ...

  7. POJ 1383 Labyrinth (树的直径求两点间最大距离)

    Description The northern part of the Pyramid contains a very large and complicated labyrinth. The la ...

  8. poj1985 / poj2631(树的直径)

    poj1985 Cow Marathon 树的直径裸题 树的直径的一般求法: 任意一点为起点,dfs/bfs找出与它最远的点$u$ 以$u$为起点,dfs/bfs找出与它最远的点$v$ 则$d(u,v ...

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

    题意如上,含有重边(重边的话,俩个点就可以构成了边双连通). 先缩点成树,在求数的直径,最远的连起来,剩下边(桥)的自然最少.这里学习了树的直径求法:第一次选任意起点U,进行bfs,到达最远的一个点v ...

随机推荐

  1. 阿里云服务器(Win 2008 R2 Standard)安装MSSM 2008 R2之1033和2052问题

    最近在给租用的阿里云服务器安装Sql Server 2008 R2 Express时,遭遇下面的问题.经过几番折腾后,终于解决问题,完成安装,这里总结分享我的解决方法,希望能给遇到相同问题的小伙伴们节 ...

  2. 推送消息实现icon角标的动态显示

    在你自己服务器上做计数,客户端做减法并反馈给你的服务器 ,然后你服务器将需要显示的数字发送给苹果推送服务器(就是消息中的badge)比如:1,你服务器上发送出去3个推送消息到A手机           ...

  3. Codeforces 549C The Game Of Parity(博弈)

    The Game Of Parity Solution: 这个题只需要分类讨论就可以解决. 先分别统计奇数和偶数的个数. 然后判断谁走最后一步,如果走最后一步时候同时有偶数和奇数,那么走最后一步的赢. ...

  4. (转)Android开发:性能最佳实践-管理应用内存

    翻自:http://developer.android.com/training/articles/memory.html 在任何软件开发环境中,RAM都是宝贵的资源,但在移动操作系统中更加珍贵.尽管 ...

  5. 【随记】关于List集合用Linq GroupBy分组过后的遍历小记

    List<LeaderKaoQin> lstLeader = new List<LeaderKaoQin>();//一个List集合IGrouping<string, L ...

  6. python的py文件打包成exe

    一.首先需要安装Pyinstaller-- 使用pip来安装模块 (我电脑上装的是python的一个编译环境Anaconda,如果电脑上装的是python自带的IDE的话,就直接进入python的安装 ...

  7. js中对象调用对象中的方法

    var o = {a:"abc", b:{ c:function(param){ alert(this.a); //这里的this指向的不是o而是b,所以this是没有a属性的,这 ...

  8. C语言实现五子棋简单功能

    /******************************************************************** C-4.29-1: 实现五子棋游戏 操作说明:用方向键或者& ...

  9. Successfully installed matplotlib

    Installing /usr/local/lib/python2.7/dist-packages/matplotlib-1.4.0-py2.7-nspkg.pthSuccessfully insta ...

  10. 我的前端之旅--SeaJs基础和spm编译工具运用[图文]

    标签:seajs   nodejs   npm   spm   js 1. 概述 本文章来源于本人在项目的实际应用中写下的记录.因初期在安装和使用Seajs和SPM的时候,有点不知所措的经历.为此,我 ...