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);
这里的ss代表是否i在first s的路径上,ss=second s+1,否则ss=first i+1.

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的更多相关文章

  1. codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径

    题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...

  2. codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点

    J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...

  3. [J]computer network tarjan边双联通分量+树的直径

    https://odzkskevi.qnssl.com/b660f16d70db1969261cd8b11235ec99?v=1537580031 [2012-2013 ACM Central Reg ...

  4. SGU 149. Computer Network( 树形dp )

    题目大意:给N个点,求每个点的与其他点距离最大值 很经典的树形dp...很久前就想写来着...看了陈老师的code才会的...mx[x][0], mx[x][1]分别表示x点子树里最长的2个距离, d ...

  5. (中等) CF 555E Case of Computer Network,双连通+树。

    Andrewid the Android is a galaxy-known detective. Now he is preparing a defense against a possible a ...

  6. Computer Network Homework2’s hard question

    Computer Network Homework2’s hard question 2. What is the signal which is used to modulate the origi ...

  7. Computer Network Homework3’ s hard question

    Computer Network Homework3’ s hard question 1. Which kind of protocol does CSMA belong to? A. Random ...

  8. [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分)

    [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分) 题面 给出一个无向图,以及q条有向路径.问是否存在一种给边定向的方案,使得 ...

  9. Sgu149 Computer Network

    Sgu149 Computer Network 题目描述 给你一棵N(N<=10000)个节点的树,求每个点到其他点的最大距离. 不难想到一个节点到其他点的最大距离为:max(以它为根的子树的最 ...

  10. computer network layers architecture (TCP/IP)

    computer network layers architecture (TCP/IP) 计算机网络分层架构 TCP/IP 协议簇 OSI 模型(7 层) TCP/IP (4 层) Applicat ...

随机推荐

  1. 断言函数-RF

    测试用例的目的是要验证一些操作否符合我们的预期结果,所以在测试用例中,断言函数是必不可少的一项.我们做的每一步操作都会有预期的结果,为了保证操作得到的结果符合预期,我们需要在测试用例中添加断言,来保证 ...

  2. eslint 的 使用常见问题(一)

    在source tree 远程拉去一个项目,然后无缘无故 代码各处飘红 ,然后看了很是烦躁.碰见一下几个问题,后续持更 一.升级es6 出现这个问题的原因:let是EcmaScript 6 里面才有的 ...

  3. Linux常用命令--不断更新

    Linux命令: !. 1.[root@loc8lhost/root]# 表示登陆进去系统,其中#是超级⽤用户也即root⽤用 户的系统提示符 #. 2.reboot命令可以重启系统 $. 3.关闭系 ...

  4. steam 数据转换

    目录 数组和集合互转 数组转集合 方法一 遍历 方法二 asList 方法三 steam 集合转数组 方法一 循环 方法二 toArray 方法三 steam 小结 string转为Character ...

  5. 使用Loadrunner进行性能测试

    一.确定性能测试的范围.要求.配置.工具等 明确测试的系统: 本文档主要指的是web应用. 明确测试要求: 用户提出性能测试,例如,网站首页页面响应时间在3S之内,主要的业务操作时间小于10s,支持3 ...

  6. [BUUOJ记录] [ACTF2020 新生赛]Include

    本题主要考查了利用php://filter伪协议进行文件包含 进入题目根据Tip进入正题,可以看到URL中存在文件包含(题目名也很直接) 首先考虑 "php://input"伪协议 ...

  7. 2vscode user settings

    {   "editor.accessibilityPageSize": 14,   "editor.fontSize": 14,   "editor. ...

  8. Zabbix Agent日志路径定位

    Zabbix Agent的日志一般记录在zabbix_agentd.log中,那么如何定位.找到Zabbix Agent的日志路径呢? 下面从Linux操作系统和Windows系统来简单总结一下,方便 ...

  9. leetcode刷题-40组合总和2

    题目 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用 ...

  10. composer 三大组成部分

    composer 三大组成部分:1. 仓库 2. 命令行下载器 3. 自动加载. 1. 仓库 公有仓库 https://packagist.org 私有仓库 https://packagist.com ...