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. hibernate课程 初探单表映射3-2 基本类型

    本节内容:(介绍基本类型) 1 数据类型 简介 2 时间类型 简介 3 时间类型 demo 1 hibernate类型 java类型   integer/int java.lang.Integer/i ...

  2. 跨平台移动开发phonegap/cordova 3.3全系列教程-开发环境搭建

    操作系统:windwos xp 1.   安装JDK 打开如下网站下载JDK http://www.oracle.com/technetwork/java/javase/downloads/index ...

  3. 【extjs6学习笔记】0.4 准备: 书籍与文档

    Ext JS 6 By Example Ext JS Essentials Learning Ext JS - Fourth Edition Ext JS 6: Getting Started htt ...

  4. CentOS-7.3.1611编译安装 Nginx-1.12.1+mysql-5.7.19+PHP-7.1.8+zabbix-3.4.1

    CentOS-7.3.1611编译安装 Nginx-1.12.1+mysql-5.7.19+PHP-7.1.8+zabbix-3.4.1 下载软件 1.下载nginx http://nginx.org ...

  5. Python 加持,给你更有趣的 Azure 虚拟机开关重启方法!

    在程序员的世界里,有关编程语言孰优孰劣的争论从来就没有消停过,不管你更粉哪种语言,毫无疑问,每种语言都有自己擅长的领域,而一些语言因为上手简单.扩展性强.功能强大等因素,往往会比较多地出现在我们面前, ...

  6. EF ObjectQuery查询及方法

      string esql = "select value c from NorthwindEntities.Customers as c order by c.CustomerID lim ...

  7. 数据类型 -- uint32_t 类型

    整型的每一种都有无符号(unsigned)和有符号(signed)两种类型(float和double总是带符号的),在默认情况下声明的整型变量都是有符号的类型(char有点特别),如果需声明无符号类型 ...

  8. ASP.NET中登陆验证码的生成和输入验证码的验证

    一:验证码的生成实现代码 protected void Page_Load(object sender, EventArgs e)    {        string validateCode = ...

  9. TP5.0:同一个控制器访问不同方法

    首先,我把TP框架的内容放置在manualtp5文件夹 在manualtp5/application/index/controller/index控制器中定义两个方法: 我们都知道,如果我们网址中不输 ...

  10. PHP实现文件上传和下载(单文件上传、多文件上传、多个单文件上传)(面向对象、面向过程)

    今天我们来学习用PHP进行文件的上传和下载,并且用面向过程和面向对象的方式对文件上传进行一个限制 一.简单的上传测试 1.客户端:upload.php 2.后端:doAction.php 结果: 二. ...