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. ios专题 - socket(1)

    二,BSD socket API 简介 BSD socket API 和 winsock API 接口大体差不多,下面将列出比较常用的 API: API接口 讲解 int socket(int add ...

  2. Fatal error: Uncaught SoapFault exception

    Warning: SoapClient::SoapClient() expects parameter 2 to be array, boolean given in  login\login.php ...

  3. 登录超时,给出提示跳到登录页面(ajax、导入、导出)

    一.一般页面登录超时验证,可以用过滤器filter,如下: package com.lg.filter; import java.io.IOException; import javax.servle ...

  4. js获取上传文件信息并及时查看

    <form id="picForm" name="picForm"  method="post" enctype="mult ...

  5. jQuery 获取父元素、子元素、同级元素

    详情:http://www.w3school.com.cn/jquery/jquery_traversing_ancestors.asp   parent() 方法返回被选元素的直接父元素.(常用) ...

  6. WPF Navigation导航

    WPF导航这个话题,网上的解决方法有很多种,有点吃猪脚的感觉,弃之可惜,食之乏味. 不过还是简单聊聊吧. 常见的导航: 利用HyperLink导航,可以到某一个Page页面,也可以是外部链接,当然也可 ...

  7. linq 多个left join 和 sql union all -> linq union 方法

     (   from s in Base_SysMenus   join r in Base_RoleRights on s.Menu_Id equals r.Menu_Id into temp   f ...

  8. iOS 图片转NSData-b

    iOS开发中 UIImage可能经常需要转为NSData 上传 传递等等 有两个比较常用的方法 UIImageJPEGRepresentation UIImagePNGRepresentation 第 ...

  9. WPF的模版

    此例子来自<深入浅出WPF>,刘铁猛. VS2010 源码1:不使用Bingding 源码2:使用Binding 下面展示一些代码: 主窗体XAML代码: <Window x:Cla ...

  10. Musical Theme

    poj1743:http://poj.org/problem?id=1743 题意:题意抽象出来就是给你一个序列,然后找一个长度不少于5的没有重复的等差数列. 题解:每相邻的两个数做差,然后转化成求字 ...