【题解】Computer Network
Description
给你一棵N(N<=10000)个节点的树,求每个点到其他点的最大距离。
Input
第一行一个数N。
接下来若干行每行两个数k,t描述一条点k到点t的边(输入数据保证无重复边)。
Output
N行每行一个数表示每个点到其他点的最大距离。
Sample Input
5
1 2
1 3
1 4
4 5
Sample Output
2
3
3
2
3
解题思路
【TREE DP】对于每个数i,记录两个值first i和second i,起点标记中点为i,终点是某个叶子结点的次短路或者最短路。两条路径i点没有公共点,在记录深点a i.
f i=max(first i,up s+1,ss);up i=max(up s+1,ss);
HINT
(参考代码,非本人)
#include <cstdio>
#include <algorithm> const int maxn=20000; int deep[maxn+10],first[maxn+10],second[maxn+10],maxlen[maxn+10],n;
int pre[(maxn<<1)+10],now[maxn+10],son[maxn+10],tot,up[maxn+10]; int ins(int a,int b)
{
tot++;
pre[tot]=now[a];
now[a]=tot;
son[tot]=b;
return 0;
} int dfs(int u,int fa)
{
int j=now[u];
deep[u]=deep[fa]+1;
while(j)
{
int v=son[j];
if(v!=fa)
{
dfs(v,u);
if(first[v]+1>second[u])
{
second[u]=first[v]+1;
}
if(first[v]+1>first[u])
{
second[u]=first[u];
first[u]=first[v]+1;
}
}
j=pre[j];
}
return 0;
} int getans(int u,int fa)
{
maxlen[u]=std::max(first[u],up[fa]+1);
up[u]=up[fa]+1;
if(first[u]+1==first[fa])
{
maxlen[u]=std::max(maxlen[u],second[fa]+1);
up[u]=std::max(up[u],second[fa]+1);
}
else
{
maxlen[u]=std::max(maxlen[u],first[fa]+1);
up[u]=std::max(up[u],first[fa]+1);
}
int j=now[u];
while(j)
{
int v=son[j];
if(v!=fa)
{
getans(v,u);
}
j=pre[j];
}
return 0;
} int main()
{
scanf("%d",&n);
for(int i=1; i<n; i++)
{
int a,b;
scanf("%d%d",&a,&b);
ins(a,b);
ins(b,a);
}
up[0]=-1;
first[0]=-1;
second[0]=-1;
dfs(1,0);
getans(1,0);
for(int i=1; i<=n; i++)
{
printf("%d\n",maxlen[i]);
}
return 0;
}
【题解】Computer Network的更多相关文章
- codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径
题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...
- codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点
J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...
- [J]computer network tarjan边双联通分量+树的直径
https://odzkskevi.qnssl.com/b660f16d70db1969261cd8b11235ec99?v=1537580031 [2012-2013 ACM Central Reg ...
- SGU 149. Computer Network( 树形dp )
题目大意:给N个点,求每个点的与其他点距离最大值 很经典的树形dp...很久前就想写来着...看了陈老师的code才会的...mx[x][0], mx[x][1]分别表示x点子树里最长的2个距离, d ...
- (中等) CF 555E Case of Computer Network,双连通+树。
Andrewid the Android is a galaxy-known detective. Now he is preparing a defense against a possible a ...
- Computer Network Homework2’s hard question
Computer Network Homework2’s hard question 2. What is the signal which is used to modulate the origi ...
- Computer Network Homework3’ s hard question
Computer Network Homework3’ s hard question 1. Which kind of protocol does CSMA belong to? A. Random ...
- [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分)
[Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分) 题面 给出一个无向图,以及q条有向路径.问是否存在一种给边定向的方案,使得 ...
- Sgu149 Computer Network
Sgu149 Computer Network 题目描述 给你一棵N(N<=10000)个节点的树,求每个点到其他点的最大距离. 不难想到一个节点到其他点的最大距离为:max(以它为根的子树的最 ...
- computer network layers architecture (TCP/IP)
computer network layers architecture (TCP/IP) 计算机网络分层架构 TCP/IP 协议簇 OSI 模型(7 层) TCP/IP (4 层) Applicat ...
随机推荐
- jQuery捕获-获取DOM元素内容和属性
一.获取内容 1.text()-设置或返回所选元素的文本内容 2.html()-设置或返回所选元素的内容(包括HTML标记) 3.val()-设置或 返回表单字段的值 $(document).read ...
- elementui table fixed错位
目前在官方没有解决这个bug前如果有人碰到类似问题.建议大家监听table数据,每次数据得到或改变的时候去重新渲染一下 /* 监听table数据对象 */ watch: { tableDa ...
- Java Android程序员软件开发知识:枚举的介绍,以及代码的编写教程。
Java枚举 Java 5.0引入了枚举,枚举限制变量只能是预先设定好的值.使用枚举可以减少代码中的bug. 例如,我们为果汁店设计一个程序,它将限制果汁为小杯.中杯.大杯.这就意味着它不允许顾客点除 ...
- 9.深入k8s:调度器及其源码分析
转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com 源码版本是1.19 这次讲解的是k8s的调度器部分的代码,相对来说比较复杂,慢慢的梳理清 ...
- 小程序开发-小程序tabBar不显示的原因分析
在尝试小程序开发时,tabBar这个是个非常常见的组件,但是今天在进行开发时,设置了TabBarb并没有显示,被这个问题困扰了近半小时,现在将排查问题后所得到的经验记录下来. 首先 如果tabBar不 ...
- 【BIM】基于BIMFACE的空间拆分与合并
BIMFACE中矩形空间拆分与合并 应用场景 在BIM运维场景中,空间同设备一样,作为一种资产被纳入运维管理体系,典型的应用场景例如商铺.防火分区等,这就涉及到空间的拆分和合并,在bimface中,已 ...
- Count(*) 与 count(field) 一样吗?
有这么个表: 执行 select count(*) from hy_test select count(deptno) from hy_test 都得到 5 但执行 select count(name ...
- weekly-contest-205
weekly-contest-205 1 / 5507. 替换所有的问号 class Solution { public String modifyString(String s) { StringB ...
- RabbitMQ和Kafka的高可用集群原理
前言 小伙伴们,通过前边文章的阅读,相信大家已经对RocketMQ的基本原理有了一个比较深入的了解,那么大家对当前比较常用的RabbitMQ和Kafka是不是也有兴趣了解一些呢,了解的多一些也不是坏事 ...
- 不用写代码也能做表单 —— 加载meta即可
做增删改查要写多少代码? 一个表单一套代码,十个表单十套代码吗? 我这么懒,怎么会写这么多代码? 我想做到:即使一百个表单也只需要一套代码(而且不需要复制粘贴).实现多个表单,只需要加载不同的meta ...