hdu2196 Compute
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4646 Accepted Submission(s): 2345
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.
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<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define maxn 10060
struct node{
int len,to,next;
}e[2*maxn];
int first[maxn],vis[maxn],dist1[maxn],dist2[maxn],ans[maxn];//dist1[i]表示最大路,dist2[i]表示次大路,dist1id[i]表示得到最大路的叶子节点编号,dist2id[i]表示得到次大路的叶子节点编号
int dist1id[maxn],dist2id[maxn];
void dfs(int u)
{
int i,j,t1,t2,flag;
t1=t2=0;
vis[u]=1;
flag=0;
for(i=first[u];i!=-1;i=e[i].next){
int v=e[i].to;
if(vis[v])continue;
flag=1;
dfs(v);
int t=e[i].len+dist1[v];
if(t>=dist1[u]){
dist2[u]=dist1[u];
dist1[u]=t;
dist2id[u]=dist1id[u];
dist1id[u]=dist1id[v];
}
else if(t>dist2[u]){
dist2[u]=t;
dist2id[u]=dist1id[v];
}
}
if(flag==0){
dist1[u]=dist2[u]=0;
dist1id[u]=dist2id[u]=u;return;
}
}
void dfs1(int u)
{
int i,j,t,t1,t2;
t1=t2=0;
vis[u]=1;
for(i=first[u];i!=-1;i=e[i].next){
int v=e[i].to;
if(vis[v])continue;
if(dist1id[u]==dist1id[v]){ //这里先看一下父亲节点所求出的最大路的叶子节点编号是不是和当前节点相同
int t=e[i].len+dist2[u];
if(t>=dist1[v]){
dist2[v]=dist1[v];
dist2id[v]=dist1id[v];
dist1[v]=t;
dist1id[v]=dist2id[u];
}
else if(t>dist2[v]){
dist2[v]=t;
dist2id[v]=dist2id[u];
} //要随时更新dist1[i],这里dist1[i]已经不仅是子树范围了,而是全部范围
}
else{
int t=e[i].len+dist1[u];
if(t>=dist1[v]){
dist2[v]=dist1[v];
dist2id[v]=dist1id[v];
dist1[v]=t;
dist1id[v]=dist1id[u];
}
else if(t>dist2[v]){
dist2[v]=t;
dist2id[v]=dist1id[u];
}
}
dfs1(v);
}
}
int main()
{
int n,m,i,j,c,d;
while(scanf("%d",&n)!=EOF)
{
memset(first,-1,sizeof(first));
int tot=0;
for(i=2;i<=n;i++){
scanf("%d%d",&c,&d);
int u,v;
u=i;v=c;
tot++;
e[tot].next=first[u];e[tot].to=v;e[tot].len=d;
first[u]=tot;
tot++;
e[tot].next=first[v];e[tot].to=u;e[tot].len=d;
first[v]=tot;
}
memset(vis,0,sizeof(vis));
memset(dist1,0,sizeof(dist1));
memset(dist2,0,sizeof(dist2));
memset(dist1id,0,sizeof(dist1id));
memset(dist2id,0,sizeof(dist2id));
dfs(1);
memset(vis,0,sizeof(vis));
dfs1(1);
for(i=1;i<=n;i++){
printf("%d\n",dist1[i]);
}
}
return 0;
}
hdu2196 Compute的更多相关文章
- C#中DataTable中的Compute方法使用收集
原文: C#中DataTable中的Compute方法使用收集 Compute函数的参数就两个:Expression,和Filter. Expresstion是计算表达式,关于Expression的详 ...
- Compute Resource Consolidation Pattern 计算资源整合模式
Consolidate multiple tasks or operations into a single computational unit. This pattern can increase ...
- 学习OpenStack之(6):Neutron 深入学习之 OVS + GRE 之 Compute node 篇
0.环境 硬件环境见上一篇博客:学习OpenStack之(5):在Mac上部署Juno版本OpenStack 四节点环境 OpenStack网络配置:一个tenant, 2个虚机 Type drive ...
- openstack-lanch an instance and nova compute log analysis
1. how to launch an instance: [root@localhost ~(keystone_admin)]# nova flavor-list+----+-----------+ ...
- 【原创翻译】初识Unity中的Compute Shader
一直以来都想试着自己翻译一些东西,现在发现翻译真的很不容易,如果你直接把作者的原文按照英文的思维翻译过来,你会发现中国人读起来很是别扭,但是如果你想完全利用中国人的语言方式来翻译,又怕自己理解的不到位 ...
- Many2one类型的fields Compute得到的值 搜索
v8 默认情况下compute的值不存储于数据库中,在高级搜索中也不可以进行搜索 想要对这种类型的值搜索,需要在field的定义中添加search参数,在search的函数中编写搜索逻辑. 例子: r ...
- A trip through the Graphics Pipeline 2011_13 Compute Shaders, UAV, atomic, structured buffer
Welcome back to what’s going to be the last “official” part of this series – I’ll do more GPU-relate ...
- DataTable.Compute()用法
DataTable.Compute()用法 2010-04-07 11:28 一.DataTable.Compute()方法說明如下 作用: 计算用来传递筛选条件的当前行上的给定表达 ...
- 转:DataTable的Compute方法的应用
转自:http://www.cnblogs.com/hfliyi/archive/2013/01/08/2851944.html 项目中遇到计算平均值.标准偏差.平均值+标准偏差.平均值+2倍标准偏差 ...
随机推荐
- 【Java】集合综合案例 - 播放器管理
集合综合案例 文章目录 集合综合案例 需求分析 项目演示 详细设计 代码实现 歌曲类 播放器类 播放列表类 测试 参考资料 播放器管理 需求分析 项目演示 详细设计 代码实现 重新搞一波 复习巩固 简 ...
- 【Linux】nohup和&的区别
同样都是后台执行进程,但是nohup和&有什么区别呢? & 是指后台运行: nohup 的功能和& 之间的功能并不相同. 其中,nohup 可以使得命令永远运行下去和用户终端没 ...
- 干货!上古神器 sed 教程详解,小白也能看的懂
目录: 介绍工作原理正则表达式基本语法数字定址和正则定址基本子命令实战练习 介绍 熟悉 Linux 的同学一定知道大名鼎鼎的 Linux 三剑客,它们是 grep.awk.sed,我们今天要聊的主角就 ...
- Numpy的一些学习记录
Numpy的一些记录 产生numpy.array的方式 import numpy as np arr1 = np.array([1, 2, 3]) print(arr1) arr2 = np.zero ...
- 前端面试之CSS权重问题!
前端面试之CSS权重问题! 下面的权重按照从小到大来排列! 1.通用选择器(*) 2.元素(类型)选择器 权重1 3.类选择器 权重10 4.属性选择器 5.伪类 6.ID 选择器 权重100 7.内 ...
- 多路复用器Select、Poll、Epoll区别梳理
注意:本文是本人的学习总结,可能存在理解上的错误,请带着怀疑眼光看待,如果有不准确的地方欢迎指出,疑义相与析.为了叙述完整性,前面有一些前置知识,可以根据目录直接看后面的详解部分. 前置知识 用户态与 ...
- Hash Join: Basic Steps
Joins https://docs.oracle.com/database/121/TGSQL/tgsql_join.htm#TGSQL242 tidb/index_lookup_hash_join ...
- udp 连接
在今天的内容里,我对 UDP 套接字调用 connect 方法进行了深入的分析.之所以对 UDP 使用 connect,绑定本地地址和端口,是为了让我们的程序可以快速获取异步错误信息的通知,同时也可以 ...
- VMware 虚拟机逃逸漏洞
所谓虚拟机逃逸(Escape Exploit),指的是突破虚拟机的限制,实现与宿主机操作系统交互的一个过程,攻击者可以通过虚拟机逃逸感染宿主机或者在宿主机上运行恶意软件. 针对 VMware 的虚拟机 ...
- 一文带你看遍 JDK9~14 的重要新特性!
Java9 发布于 2017 年 9 月 21 日 .作为 Java8 之后 3 年半才发布的新版本,Java 9 带 来了很多重大的变化其中最重要的改动是 Java 平台模块系统的引入,其他还有诸如 ...