【题解】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 ...
随机推荐
- VUE响应式原理-如何追踪变化
Vue 最独特的特性之一,是其非侵入性的响应式系统.数据模型仅仅是普通的 JavaScript 对象.而当你修改它们时,视图会进行更新.这使得状态管理非常简单直接 如何追踪变化 当你把一个普通的 Ja ...
- 我用 Java 8 写了一段逻辑,同事直呼看不懂,你试试看。。
业务背景 首先,业务需求是这样的,从第三方电商平台拉取所有订单,然后保存到公司自己的数据库,需要判断是否有物流信息,如果有物流信息,还需要再进行上传. 而第三方接口返回的数据是 JSON 格式的,其中 ...
- @PostConstruct详解
一.定义: @PostContruct是spring框架的注解,在方法上加该注解会在项目启动的时候执行该方法,也可以理解为在spring容器初始化的时候执行该方法. 从Java EE5规范开始,Ser ...
- MonoBehaviour生命周期与对象数据池应用
预热游戏对象: tempObject = Instantiate(cubePrefab) as GameObject ; tempObject .SetActive( false ); 游戏对象tem ...
- MySql密码的问题
由于长时间没使用过MySql了,也由于之前没有做笔记的习惯,晚上因为MySQL的密码问题导致数据库长时间没连上.纠结了这么久还是决定记录下来,毕竟安装的东西多了,这年头到处都是密码,加上时间一长,很容 ...
- 查看Linux虚拟机是什么架构
uname -a 可以看出此虚拟机是x86架构,64位
- elo system
今天了解了一下游戏中的PVP模块的实现,大多数的游戏都使用到了ELO算法,刚开始的时候并不清楚这个算法是做什么的,对此开始大量查找有关于ELO算法的资源,功夫不负有心人,总算找到一些有用的资源了. 先 ...
- oldboy edu python full stack s22 day16 模块 random time datetime os sys hashlib collections
今日内容笔记和代码: https://github.com/libo-sober/LearnPython/tree/master/day13 昨日内容回顾 自定义模块 模块的两种执行方式 __name ...
- defer implement for C/C++ using GCC/Clang extension
前述: go 中defer 给出了一种,延时调用的方式来释放资源.但是对于C/C++去没有内置的这种属性.对于经常手动管理内存的C/C++有其是C程序员这种特性显得无比重要.这里给出了一种基于GCC/ ...
- IDEA使用 live template添加groovy脚本给方法,类,js方法添加注释(转载)
IDEA添加Live Template: File->Setting->Editor->Live Templates Abbreviation: * Template text: * ...