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. 机器学习框架ML.NET学习笔记【1】基本概念与系列文章目录

    一.序言 微软的机器学习框架于2018年5月出了0.1版本,2019年5月发布1.0版本.期间各版本之间差异(包括命名空间.方法等)还是比较大的,随着1.0版发布,应该是趋于稳定了.之前在园子里也看到 ...

  2. phpcms如何调用某一组图里的所有图片

    {pc:get sql="select * from v9_picture_data where id = '$id'"} {loop $data $n $r} {loop str ...

  3. Docker | 第七章:Docker Compose服务编排介绍及使用

    前言 前面章节,我们学习了如何构建自己的镜像文件,如何保存自己的镜像文件.大多都是一个镜像启动.当一个系统需要多个子系统进行配合时,若每个子系统也就是镜像需要一个个手动启动和停止的话,那估计实施人员也 ...

  4. 整理:sql server 中sql语句执行顺序

    SQL Server 查询处理中的各个阶段(SQL执行顺序) SQL 不同于与其他编程语言的最明显特征是处理代码的顺序.在大数编程语言中,代码按编码顺序被处理,但是在SQL语言中,第一个被处理的子句是 ...

  5. KendoUI 自定义验证:

    Html: <label>@LogicNameAttribute.GetLogicName(typeof(Reward).GetProperty("ExtraRewardMone ...

  6. 记DotNetBar换肤

    界面: comboBoxEx  选择皮肤 buttonX 测试指定皮肤 styleManager 后台代码: 初始化 : this.EnableGlass = false; 设置窗体效果 不设置 依然 ...

  7. C 碎片二 数据类型

    一.概述 C 语言包含的数据类型如下图所示: 二.各种数据类型介绍 2.1 整型 整形包括短整型.整形和长整形. 2.1.1 短整形 short a=1; 2.1.2 整形 一般占4个字节(32位), ...

  8. Maven建立spring-web项目

    参考博客网址: https://blog.csdn.net/caoxuekun/article/details/77336444 1.eclipse集成maven 2.maven创建web项目 3.搭 ...

  9. JavaScript_HTML DEMO_1_概念

    HTML DOM - 文档对象模型 当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model). 1. 通过可编程的对象模型,JavaScript获得了足够的能力来创 ...

  10. 利用ajax实现分页效果

    在网页中看到的分页效果,想一下就点击分页中的内容的时候,然后调用ajax调出对应的数据,正确的显示在相应的标签内. 1.用html实现正确的样式和结构 2.采用jquery中的ajax调出数据. 需要 ...