Computer HDU - 2196

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.

InputInput 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.OutputFor 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 题意:一棵树,问某一个点能够走的不重复点的最长的路径是多少;
思路:先随便找一个点跑一遍树的直径,然后直径的两头各跑一遍dfs
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdlib>
#include <vector>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = ; struct Edge {
int u,v,next,len;
}edge[maxn]; int n;
int tot;
int head[maxn];
bool vis[maxn];
void addedge(int u,int v,int w)
{
edge[tot].u = u;
edge[tot].v = v;
edge[tot].len = w;
edge[tot].next = head[u];
head[u] = tot++;
}
int dfs(int start,int d[])
{
for(int i = ;i <= n;i++)
d[i] = INF;
memset(vis,false,sizeof vis);
d[start] = ;
vis[start] = true;
queue<int>que;
que.push(start);
int maxx = ;
int pos;
while(!que.empty())
{
int p = que.front();
que.pop();
vis[p] = false;;
if(maxx < d[p])
{
maxx = d[p];
pos = p;
}
for(int i=head[p];i!=-;i=edge[i].next)
{
int v = edge[i].v;
if(d[v] > d[p] + edge[i].len)
{
d[v] = d[p] + edge[i].len;
if(!vis[v])
{
que.push(v);
vis[v] = true;
}
}
}
}
return pos;
}
int main()
{
while(~scanf("%d",&n))
{
int d1[maxn],d2[maxn];
memset(head,-,sizeof head);
tot = ;
for(int u = ;u <= n;u++)
{
int v,w;
scanf("%d %d",&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
int st = dfs(,d1);
int en = dfs(st,d1);
dfs(en,d2);
for(int i=;i<=n;i++)
printf("%d\n",max(d1[i],d2[i]));
}
}

Computer HDU - 2196的更多相关文章

  1. 树形dp(B - Computer HDU - 2196 )

    题目链接:https://cn.vjudge.net/contest/277955#problem/B 题目大意:首先输入n代表有n个电脑,然后再输入n-1行,每一行输入两个数,t1,t2.代表第(i ...

  2. HDU 2196 树形DP Computer

    题目链接:  HDU 2196 Computer 分析:   先从任意一点开始, 求出它到其它点的最大距离, 然后以该点为中心更新它的邻点, 再用被更新的点去更新邻点......依此递推 ! 代码: ...

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

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

  4. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

  5. HDU 2196 Computer (树dp)

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

  6. HDU 2196树形DP(2个方向)

    HDU 2196 [题目链接]HDU 2196 [题目类型]树形DP(2个方向) &题意: 题意是求树中每个点到所有叶子节点的距离的最大值是多少. &题解: 2次dfs,先把子树的最大 ...

  7. HDU 2196.Computer 树形dp 树的直径

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

  8. HDU 2196 Computer 树形DP经典题

    链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...

  9. hdu 2196 computer

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

随机推荐

  1. linux下mysql-5.5.27.tar.gz源程序包安装实例

    研究了好几天,终于把mysql装上了,现在来做下小结. 系统环境:fedora8 虚拟机. 1.检查安装使用的编译工具gcc是否存在,如果不存在则要下载安装 # gcc -v 2.卸载低版本的mysq ...

  2. 使用pycharm 运行python的django项目时报错“Quit the server with CTRL-BREAK.”

    Quit the server with CTRL-BREAK.Error: [Errno 10013] 1昨晚测试时还好好的,怎么突然出现这个错误,于是GOOLE,找到个帖子说可能是端口占用了,用工 ...

  3. hibernate课程 初探单表映射1-11 通过hibernate API访问编写第一个小例子

    hibernate 业务流程 1 创建配置对象 Configuration config  = new  Configuration().configure(); 2 创建服务注册对象 Service ...

  4. https微信分享看不到图片的坑

    最近在做一个活动项目的时候一开始走的http,发现网络被劫持的特别严重,没办法,只能改走https,但是修改为https后发现在使用微信js-sdk分享的时候看不到缩略图,直接通过地址打开是可以找开图 ...

  5. 使用纯css实现波浪效果

    有时候我们需要实现水晃动的效果,其实我们可以通过css旋转动画和圆角来实现. 首先来2个div,外层div相对定位,内层div绝对定位,内层div大致位于外层div上半部分.外层div设置一个颜色较深 ...

  6. hibernate笔记4--qbc查询

    Criteria:是一种完全面向对象的查询方式,Criteria查询也叫做qbc查询(query by Criteria).         查询全部,分页查询,统计查询,条件查询,排序查询,离线查询 ...

  7. 在window下, Java调用执行bat脚本

    参考博客: https://www.cnblogs.com/jing1617/p/6430141.html 最近一段时间用到了Java去执行window下的bat脚本, 这里简单记录一下: 我这里是先 ...

  8. iOS - 协议实现的例子

    在实际开发中,协议的应用非常广泛,以下是实际应用的例子. 1.协议的定义: myProtocolDelegate.h // // myProtocolDelegate.h // zlwPlayerAp ...

  9. COGS 750. 栅格网络流

    ★★☆   输入文件:flowa.in   输出文件:flowa.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] Bob 觉得一般图的最大流问题太难了,他不知道如何解决 ...

  10. git入门使用摘录

    无论使用github或者gitlab,第一步都是在本地生产ssh-key,ssh-key作为客户端的身份证存放在user用户的.ssh文件夹下.如果之前没有生产过,需要用ssh-keygen命令生成. ...