Computer

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6378    Accepted Submission(s): 3211

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
 
 
Author
scnu
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1011 3456 1520 2242 1059 
题意:给n个点,1为根节点,给你的是棵树,然后找到各个节点离树上其他节点最远的距离;
思路:树形dp;
因为这是一颗树,除了根节点外其他每个节点都又且一个父亲节点,首先dfs更新每个节点到其子节点下端的最长距离和次长距离,然后最长距离还可能从父亲节点那更新。那么再dfs从父亲节点更新子节点的最长距离,这个时候,父亲节点的最长可能是通过当前要更新的子节点,那么就用次长路去更新。复杂度O(n);
  1 #include<iostream>
2 #include<string.h>
3 #include<algorithm>
4 #include<queue>
5 #include<math.h>
6 #include<stdlib.h>
7 #include<stack>
8 #include<stdio.h>
9 #include<ctype.h>
10 #include<map>
11 #include<vector>
12 #include<map>
13 using namespace std;
14 typedef struct acc
15 {
16 int id;
17 int val;
18 } ak;
19 vector<ak>vec[100005];
20 bool vis[100005];
21 typedef struct node
22 {
23 int id;
24 int mcost;
25 int scost;
26 int mid;
27 int sid;
28 node()
29 {
30 mcost = 0;
31 scost = 0;
32 mid = -1;
33 sid = -1;
34 }
35 } ss;
36 ss dp[100005];
37 int father[1000005];
38 void dfs1(int n);
39 void dfs2(int n);
40 int main(void)
41 {
42 int n,m;
43 while(scanf("%d",&n)!=EOF)
44 {
45 memset(father,-1,sizeof(father));
46 int i,j;
47 memset(dp,0,sizeof(dp));
48 for(i = 0; i < 100005; i++)
49 vec[i].clear();
50 for(i = 2; i <= n; i++)
51 {
52 int id,val;
53 scanf("%d %d",&id,&val);
54 ak a;
55 a.val = val;
56 a.id = i;
57 father[i] = id;
58 vec[id].push_back(a);
59 }
60 dfs1(1);
61 dfs2(1);
62 for(i = 1; i <= n; i++)
63 {
64 printf("%d\n",dp[i].mcost);
65 }
66 }
67 return 0;
68 }
69 void dfs1(int n)
70 {
71 for(int i = 0; i < vec[n].size(); i++)
72 {
73 ak d = vec[n][i];
74 {
75 dfs1(d.id);
76 if(dp[d.id].mcost+d.val > dp[n].mcost)
77 {
78 dp[n].scost = dp[n].mcost;
79 dp[n].sid = dp[n].mid;
80 dp[n].mcost = dp[d.id].mcost+d.val;
81 dp[n].mid = d.id;
82 }
83 else if(dp[d.id].mcost+d.val > dp[n].scost)
84 {
85 dp[n].scost = dp[d.id].mcost+d.val;
86 dp[n].sid = d.id;
87 }
88 }
89 }
90 }
91 void dfs2(int n)
92 {
93 for(int i = 0; i < vec[n].size(); i++)
94 {
95 ak d = vec[n][i];
96 if(dp[n].mid!=d.id)
97 {
98 if(dp[n].mcost + d.val > dp[d.id].mcost)
99 {
100 dp[d.id].scost = dp[d.id].mcost;
101 dp[d.id].sid = dp[d.id].mid;
102 dp[d.id].mcost = dp[n].mcost + d.val;
103 dp[d.id].mid = n;
104 }
105 else if(dp[n].mcost + d.val > dp[d.id].scost)
106 {
107 dp[d.id].scost = dp[n].mcost + d.val;
108 dp[d.id].sid = n;
109 }
110 }
111 else
112 {
113 if(dp[n].scost + d.val > dp[d.id].mcost)
114 {
115 dp[d.id].scost = dp[d.id].mcost;
116 dp[d.id].sid = dp[d.id].mid;
117 dp[d.id].mcost = dp[n].scost + d.val;
118 dp[d.id].mid = n;
119 }
120 else if(dp[n].scost + d.val > dp[d.id].scost)
121 {
122 dp[d.id].scost = dp[n].scost + d.val;
123 dp[d.id].sid = n;
124 }
125 }
126 dfs2(d.id);
127 }
128 }
 

computer(hdu2196)的更多相关文章

  1. Computer(HDU2196+树形dp+树的直径)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2196 题目: 题意:有n台电脑,每台电脑连接其他电脑,第i行(包括第一行的n)连接u,长度为w,问你每 ...

  2. [HDU2196]Computer(DP)

    传送门 题意 给出一棵树,求离每个节点最远的点的距离 思路 对于我这种菜鸡,真是难啊. 每个点的距离它最远的点,除了在它子树中的,还有在它子树之外的,所以这几个状态都得表示出来. 我们能够很简单的求出 ...

  3. MySQL数据库的安装与配置(windows)

    MySQL是目前最为流行的开放源码的数据库,是完全网络化的跨平台的关系型数据库系统,它是由瑞典MySQLAB公司开发,目前属于Oracle公司.任何人都能从Internet下载MySQL软件,而无需支 ...

  4. 【FSFA 读书笔记】Ch 2 Computer Foundatinons(2)

    Hard Disk Technology 1. 机械硬盘内部构造 几个重要概念:Sector(扇区),Head(读写头),Track(磁道),Cylinder(柱面). 如果一个文件比较大,磁盘的写入 ...

  5. hdu 2196(Computer 树形dp)

    A school bought the first computer some time ago(so this computer's id is 1). During the recent year ...

  6. Multiple View Geometry in Computer Vision Second Edition by Richard Hartley 读书笔记(二)

    // Chapter 2介绍的是2d下的投影变换,摘录下了以下定理 Result 2.1. The point x lies on the line l if and only if xTl = 0. ...

  7. 【FSFA 读书笔记】Ch 2 Computer Foundatinons(1)

    Data Organization 1. 进制转换. 按照正常的书写顺序写一个数字(无论多少进制),其中最左边的列称为“最高有效符号”,最右边的列称为“最低有效符号”. (The right-most ...

  8. 图像分类(三)GoogLenet Inception_v3:Rethinking the Inception Architecture for Computer Vision

    Inception V3网络(注意,不是module了,而是network,包含多种Inception modules)主要是在V2基础上进行的改进,特点如下: 将滤波器尺寸(Filter Size) ...

  9. 【Network architecture】Rethinking the Inception Architecture for Computer Vision(inception-v3)论文解析

    目录 0. paper link 1. Overview 2. Four General Design Principles 3. Factorizing Convolutions with Larg ...

随机推荐

  1. 12-Add Digits

    寻找一个数的数根,用了暴力破解的方式,时间复杂度比较高 暂未想到O(1)的方式 Given a non-negative integer num, repeatedly add all its dig ...

  2. Qt5的安装和编译

    Ubuntu18.04安装Qt5 1.配置unbuntu 和宿主机共享文件夹安装vmware-tools 2.下载 Qt  http://download.qt.io/archive/qt/ 3.修改 ...

  3. Android Bitmap 全面解析(二)加载多张图片的缓存处理

    一般少量图片是很少出现OOM异常的,除非单张图片过~大~ 那么就可以用教程一里面的方法了通常应用场景是listview列表加载多张图片,为了提高效率一般要缓存一部分图片,这样方便再次查看时能快速显示~ ...

  4. SpringMVC responseBody注解分析

    @responsebody表示该方法的返回结果直接写入HTTP response body中一般在异步获取数据时使用,在使用@RequestMapping后,返回值通常解析为跳转路径,加上@respo ...

  5. python web框架学习笔记

    一.web框架本质 1.基于socket,自己处理请求 #!/usr/bin/env python3 #coding:utf8 import socket def handle_request(cli ...

  6. 【编程思想】【设计模式】【创建模式creational】Borg/Monostate

    Python版 https://github.com/faif/python-patterns/blob/master/creational/borg.py #!/usr/bin/env python ...

  7. 实现nfs持久挂载+autofs自动挂载

    实验环境: 两台主机 node4:192.168.37.44 NFS服务器 node2:192.168.37.22 客户端 在nfs服务器,先安装nfs和rpcbind [root@node4 fen ...

  8. 【Linux】【Services】【Disks】zfs

    1. 简介: 据说zfs有去除重复数据的功能,无良人士继续要求吧samba共享盘使用的centos7上自带的xfs改成zfs,并且开启去重功能.samba配置见 http://www.cnblogs. ...

  9. 【Fastjson】Fastjson反序列化由浅入深

    Fastjson真-简-介 fastjson是由alibaba开发并维护的一个json工具,以其特有的算法,号称最快的json库 fastjson的使用 首先先创一个简单的测试类User public ...

  10. pipeline post指令

    目录 一.介绍 二.参数说明 三.使用实例 一.介绍 post步骤包含的是在整个pipeline或阶段完成后一些附加的步骤.post步骤是可选的,所以并不包含在声明式pipeline最简结构中,但这并 ...