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. ASP编码规范

    ASP编码规范(--::) 第一章 ASP编码规范通述 ASP编码分为两大部分,一部分为静态文件编码,一部分为包含服务器端脚本的动态文件编码. 静态文件编码分script编码和HTML编码两部分. 服 ...

  2. JVM虚拟机 - Class类文件结构

    概述 Class文件是一组以8位字节为基础单位的二进制流,各个数据项目严格按照顺序紧凑地排列在Class文件之中,中间没有添加任何分隔符,这使得整个Class文件中存储的内容几乎都是程序运行的必要数据 ...

  3. mysql mysqldump 本地数据库导入本地数据库的命令

    C:\Users\Administrator>mysqldump -h localhost -P 3306 -u root -proot -n -R --triggers foryou |mys ...

  4. agc007B - Construct Sequences(构造)

    题意 题目链接 给出一个$1-N$的排列$P$,构造两个数组$a, b$满足 Sol 发现我的水平也就是能做一做0-699的题.... 直接构造两个等差数列$a, b$,公差为$20000$ 然后从小 ...

  5. Android 桌面悬浮窗效果实现,仿360手机卫士悬浮窗效果

    首先是一个小的悬浮窗显示的是当前使用了百分之多少的内存,点击一下小悬浮窗,就会弹出一个大的悬浮窗,可以一键加速.好,我们现在就来模拟实现一下类似的效果. 先谈一下基本的实现原理,这种桌面悬浮窗的效果很 ...

  6. CentOs7 修复 引导启动

    一.修复MBR: MBR(Master Boot Record主引导记录): 硬盘的0柱面.0磁头.1扇区称为主引导扇区.其中446Byte是bootloader,64Byte为Partition t ...

  7. “ipconfig不是内部命令或外部命令”解决方法

    第一:用鼠标右键单击“计算机”,在弹出的下拉菜单中选择“属性”. 第二:在系统属性中选择“高级系统设置”.在系统属性对话框中找到其上方的“高级”选项卡,里面有一个“环境变量”按钮,点击进入 第三:在下 ...

  8. hihocoder 第四十周 三分求极值

    题目链接:http://hihocoder.com/contest/hiho40/problem/1 ,一道简单的三分. 题目是在直角坐标系中有一条抛物线y=ax^2+bx+c和一个点P(x,y),求 ...

  9. java Vamei快速教程17 多线程

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 多线程 多线程(multiple thread)是计算机实现多任务并行处理的一种方 ...

  10. linux 命令——28 tar

    通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是是必不可少的一个功能强大的工具.linux中最流行的tar是麻雀虽小,五脏俱全,功能强大.tar命令可以为linux的 ...