Computer (树形DP)
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
题目大意:
给定n,代表n台电脑,编号为1的是最初始的电脑,下面n-1对数字,分别编号为2~n的电脑相连电脑的编号与长度。
输出n行,求与第i台电脑最远的电脑的距离。
- #include <iostream>
- #include <algorithm>
- #include <cstring>
- using namespace std;
- int n,dp[],pre[];
- struct edge
- {
- int u,v,w,p;///u与v电脑相连,长度w,u对应的上一条边
- edge(){}
- edge(int u,int v,int w,int p):u(u),v(v),w(w),p(p){}
- }e[];
- int dfs(int u,int p)
- {
- int ans=;
- for(int i=pre[u];i!=-;i=e[i].p)
- {
- int v=e[i].v;
- if(v==p) continue;
- if(!dp[i])///没更新过
- dp[i]=dfs(v,u)+e[i].w;
- ans=max(ans,dp[i]);
- }
- return ans;
- }
- int main()
- {
- while(cin>>n)
- {
- memset(dp,,sizeof dp);
- memset(pre,-,sizeof pre);
- int x=;
- for(int i=,v,w;i<=n;i++)
- {
- cin>>v>>w;
- e[x]=edge(i,v,w,pre[i]);
- pre[i]=x++;
- e[x]=edge(v,i,w,pre[v]);
- pre[v]=x++;
- }
- for(int i=;i<=n;i++)
- cout<<dfs(i,-)<<'\n';
- }
- return ;
- }
Computer (树形DP)的更多相关文章
- HDU 2196.Computer 树形dp 树的直径
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- computer(树形dp || 树的直径)
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 2196 Computer 树形DP 经典题
给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权, ...
- HDU 2196 Computer 树形DP经典题
链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...
- hdu 2196 Computer(树形DP)
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu 2196(Computer 树形dp)
A school bought the first computer some time ago(so this computer's id is 1). During the recent year ...
- hdu-2169 Computer(树形dp+树的直径)
题目链接: Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【HDU 2196】 Computer (树形DP)
[HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...
- hdu 2196 Computer 树形dp模板题
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- hdu 2196 Computer(树形DP经典)
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
随机推荐
- 动手实现 Redux(五):不要问为什么的 reducer
经过了这么多节的优化,我们有了一个很通用的 createStore: function createStore (state, stateChanger) { const listeners = [] ...
- CF983A Finite or not?
思路: 如果p,q不互质,先把q除以p,q的最大公约数.接下来只要b中包含了所有q的质因子就可以了.可以在gcd(q, b) 不等于1的时候不断地用q除以gcd(q, b).最后如果q等于1,说明Fi ...
- jstat查看JVM的GC情况
jps(Java Virtual Machine Process Status Tool)是JDK 1.5提供的一个显示当前所有java进程pid的命令,简单实用,非常适合在linux/unix平台上 ...
- System类与两种输入流
1.System类对I/O的支持系统输出System.out.println 是利用了I/O流的模式完成的.实际是打印流PrintStream对象 System类中定义了三个操作的常量 1.标准/系统 ...
- Fire Air(华科校赛 网络赛)
题目 原题链接:https://www.nowcoder.com/acm/contest/106/L 在100000 * 10000的空地上,有n个时间点,每个时间点会在(xi,yi)上种一棵树. 定 ...
- uva11491 Erasing and Winning
边读入边处理 优化了速度一开始有想错了的地方.处理输入有点想用stringstream, 的问题在于他把字符串连续的数字作为一个整体,遇到空格才分开,所以不适用 #include<cstdio& ...
- 【转载】用Python实现端口映射功能(A/B/C内外网)
转载地址 :http://hutaow.com/blog/2014/09/08/write-tcp-mapping-program-with-python/ 有A,B,C三台计算机,A,B互通,B,C ...
- 全志T8智能汽车方案芯片参数介绍
T8处理器代表了Allwinner在智能汽车市场上的最新成就.T8适用于需要三维图形.高级视频处理.精密相机.多种连接选项和高水平系统集成的应用程序.它将把先进的消费电子体验带入未来的汽车,实现高性能 ...
- 【2019.6.2】python:json操作、函数、集合、random()等
一.json操作: json就是一个字符串,从文件中读取json,必须是json格式.j'son串中必须是双引号,不能有单引号,单引号不能转换 1.1使用: import json #使用json先引 ...
- 6 SQL 函数、谓词、CASE表达式
6 函数.谓词.CASE表达式 6-1 各种各样的函数 /* 所谓函数,就是输入某一值得到相应输出结果的功能.输入值称为参数(parameter),输出值称为返回值. 函数大致可以分为以下几种 : 算 ...