【题解】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 ...
随机推荐
- 华师2019软件专硕复试机试题最后一题G:找数
G. 找数 单点时限: 1.0 sec 内存限制: 256 MB 问题描述 输入一个整数 n( 2≤n≤10 ) ,你需要找到一些 n 位数(允许有前置 0 ,见样例),这些 n 位数均 由 0 ~ ...
- Labview学习之路(六)条件结构
条件结构位于 函数---结构 子选版中.相当于c语言的switch语句, 结构: 分支选择器 分支器标签 分支子框图 可接数据类型 布尔型 布尔常量 真和假 错误信息簇 真和假 整型数值 单值 可 ...
- C# OWC11
public void OcwChart(int[] Data,string[] DataName,string Yname,string Xname,string ChartName,string ...
- Linux常用命令--不断更新
Linux命令: !. 1.[root@loc8lhost/root]# 表示登陆进去系统,其中#是超级⽤用户也即root⽤用 户的系统提示符 #. 2.reboot命令可以重启系统 $. 3.关闭系 ...
- OpenShift Container Platform 4.3.0部署实录
本文参照红帽官方文档,在裸机安装Openshift4.3文档进行.因为只有一台64G内存的PC机,安装vmware vsphere 6.7免费版进行本测试,所以尝试在OCP官方文档要求的最低内存需求基 ...
- agumaster增加了网易数据源
- 一句Delete..In.. 删除语句的优化
实验环境: # 类别 版本 1 操作系统 Win10 2 数据库 Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit P ...
- 干货:不同场景容器内获取客户端源IP的方法
摘要:客户端和容器服务器之间可能存在多种不同形式的代理服务器,那容器中如何获取到客户端真实的源ip呢? k8s已经成为当今容器化的标准,人们在享受容器带来的高效与便利的同时,也遇到一些烦恼:客户端和容 ...
- Django 页面之间传递MySql数据表(Django八)
之前实现了页面传递多个参数,但没有实现页面间传递一整个数据表 session传递几个参数:https://blog.csdn.net/qq_38175040/article/details/10496 ...
- JS数据类型及常用操作
1.字符串 2.数字类型 3.布尔类型 4.数组类型 5.字典