hdu 2196 Computer 树的直径
Computer
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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.
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.
1 1
2 1
3 1
1 1
2
3
4
4
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=2e5+,M=1e6+,inf=1e9+;
const ll INF=1e18+,MOD=;
struct is
{
int v,w,nex;
}edge[N];
int head[N],edg;
int dis[N];
int deep,node1,node2;
void init()
{
memset(head,-,sizeof(head));
memset(dis,,sizeof(dis));
edg=;
deep=;
}
void add(int u,int v,int w)
{
edg++;
edge[edg].v=v;
edge[edg].w=w;
edge[edg].nex=head[u];
head[u]=edg;
} void dfs(int u,int fa,int val,int &node)
{
dis[u]=max(dis[u],val);
if(val>deep)
{
deep=val;
node=u;
}
for(int i=head[u];i!=-;i=edge[i].nex)
{
int v=edge[i].v;
int w=edge[i].w;
if(v==fa)continue;
dfs(v,u,val+w,node);
}
}
int main()
{
int n;
while(~scanf("%d",&n))
{
init();
for(int i=;i<=n;i++)
{
int v,w;
scanf("%d%d",&v,&w);
add(i,v,w);
add(v,i,w);
}
dfs(,-,,node1);
deep=;
dfs(node1,-,,node2);
deep=;
dfs(node2,-,,node1);
for(int i=;i<=n;i++)
printf("%d\n",dis[i]);
}
return ;
}
hdu 2196 Computer 树的直径的更多相关文章
- HDOJ 2196 Computer 树的直径
由树的直径定义可得,树上随意一点到树的直径上的两个端点之中的一个的距离是最长的... 三遍BFS求树的直径并预处理距离....... Computer Time Limit: 1000/1000 MS ...
- HDU 2196 Computer (树dp)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 给你n个点,n-1条边,然后给你每条边的权值.输出每个点能对应其他点的最远距离是多少 ...
- 【HDU 2196】 Computer(树的直径)
[HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...
- 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)
Problem Description A school bought the first computer some time ago(so this computer's id is 1). Du ...
- HDU 4123(树的直径+单调队列)
Bob’s Race Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 2196 Computer 树形DP经典题
链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...
- hdu 2196 computer
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu 2196 Computer 树形dp模板题
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
随机推荐
- !important------至高无上的宝剑
如上图,不同来源的两个样式,第一个样式设置了font-weight,第二个没有,浏览器会把它叠加在一起,即浏览器会把各个零散的整合成一个整体.第一个样式color:red,第二个样式color:blu ...
- 161012、JAVA读写文件,如何避免中文乱码
1.JAVA读取文件,避免中文乱码. /** * 读取文件内容 * * @param filePathAndName * String 如 c:\\1.txt 绝对路径 * @return boole ...
- 利用BAT批处理安装服务程序
@echo off @echo off set filename=LXServer.exe set servicename=Service1 set Frameworkdc=%SystemRoot%\ ...
- freemarker为null处理
http://599073210-qq-com.iteye.com/blog/1401731
- sqlite3内存不断增加的原因
数据库是这样设计的:用内存保存数据,以提高增删查改的速度,同时把数据写入磁盘,让数据落地. 如果不删除数据库里的数据,随着数据不断地添加到数据库,数据库越来越大,RES内存也越来越大. 见测试代码a. ...
- CMake快速入门教程:实战
转自http://blog.csdn.net/ljt20061908/article/details/11736713 0. 前言 一个多月前,由于工程项目的需要,匆匆的学习了一下cmake的使 ...
- sql截断日志
--收缩数据库 DBCC SHRINKDATABASE(fas) --截断事务日志: BACKUP LOG fas WITH NO_LOG 1.清空日志 DUMP TRANSACTION 库名 WIT ...
- php编译器
WordPress http://pan.baidu.com/s/1eQnOnv0 epp3: http://pan.baidu.com/s/1pJKFOD1 配合xampp: http://pan. ...
- OracleHelper 动软生成
using System; using System.Collections; using System.Collections.Specialized; using System.Data; usi ...
- UVa 11624,两次BFS
题目链接:http://vjudge.net/contest/132239#problem/A 题目链接:https://uva.onlinejudge.org/external/116/11624. ...