AT_scpc2026_div2_k.Cosmic Playground

入门

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

While exploring the galaxy, Navi and Hyeongtae discovered a cosmic playground! The cosmic playground consists of NN points and N−1N-1 bidirectional passages. Each point in the cosmic playground is numbered from 11 to NN. The iith passage connects points uiu_i and viv_i in both directions. It is always possible to travel between any two distinct points using only the passages. In other words, the cosmic playground has a tree structure.

Each point is connected to a single slide. For a permutation P=[P1,P2,…,PN]P=[P_1, P_2, \dots, P_N] of length NN, sliding down the slide at point ii moves you to point PiP_i.

Navi wants to slide down the slides non-stop to enjoy the thrill of speed. On the other hand, Hyeongtae is worried that if Navi travels too far, Navi might get lost in space. After much deliberation, Navi and Hyeongtae defined the risk level f(r)f(r) of point rr as follows:

  • The distance between points aa and bb is the minimum number of passages one must traverse to reach point bb from point aa using only the passages.
  • Navi starts at point ss and continues moving using only slides, while Hyeongtae watches Navi from point rr. The maximum distance between point rr and any point Navi can reach is defined as the maximum distance d(r,s)d(r, s).
  • The risk level f(r)f(r) when Hyeongtae remains at point rr is the sum of the maximum distances ∑s=1Nd(r,s)\sum\limits_{s = 1}^{N}{d(r, s)} when Navi starts from each point.

Hyeongtae wants to stay at the point where the risk level is minimized. Let us help Hyeongtae by calculating the risk level for each point.

What is a permutation? A permutation of length NN is a sequence that contains exactly one of each integer from 11 to NN. For example, [2,1,4,3][2,1,4,3] is a permutation, but [4,2,1,1][4, 2, 1, 1] and [1,2,3,5][1, 2, 3, 5] are not.

在探索银河系的过程中,Navi 和 Hyeongtae 发现了一座宇宙游乐场!该宇宙游乐场由 NN 个点和 N−1N-1 条双向通道组成。游乐场中的每个点均编号为 11 至 NN。第 ii 条通道双向连接点 uiu_i 和 viv_i。任意两个不同点之间总能仅通过这些通道相互到达。换言之,该宇宙游乐场具有树形结构。

每个点都连接着一条滑梯。对于一个长度为 NN 的排列 P=[P1,P2,…,PN]P=[P_1, P_2, \dots, P_N],从点 ii 滑下滑梯将把你传送至点 PiP_i。

Navi 想要不停歇地滑下滑梯,以体验速度带来的刺激感。而 Hyeongtae 则担心:若 Navi 行进距离过远,可能会在太空中迷路。经过反复商议,Navi 和 Hyeongtae 定义了点 rr 的风险等级 f(r)f(r) 如下:

  • 点 aa 与点 bb 之间的距离定义为:仅使用通道从点 aa 到达点 bb 所需经过的最少通道数。
  • Navi 从点 ss 出发,仅通过滑梯持续移动;Hyeongtae 则在点 rr 处观察 Navi。Navi 可达的所有点中,与点 rr 距离最大的那个距离,称为最大距离 d(r,s)d(r, s)。
  • 当 Hyeongtae 停留在点 rr 时,其风险等级 f(r)f(r) 定义为:当 Navi 分别从每个起点 s=1,2,…,Ns = 1, 2, \dots, N 出发时,对应的最大距离 d(r,s)d(r, s) 的总和,即 ∑s=1Nd(r,s)\sum\limits_{s = 1}^{N}{d(r, s)}。

Hyeongtae 希望停留在风险等级最小的那个点上。请帮助 Hyeongtae 计算每个点的风险等级。

什么是排列?一个长度为 NN 的排列是指恰好包含 11 至 NN 中每个整数各一次的序列。例如,[2,1,4,3][2,1,4,3] 是一个排列,但 [4,2,1,1][4, 2, 1, 1] 和 [1,2,3,5][1, 2, 3, 5] 不是。

输入格式

The input is given from Standard Input in the following format:

NN
P1P_1 P2P_2 …\dots PNP_N
u1u_1 v1v_1
u2u_2 v2v_2
⋮\vdots
uN−1u_{N-1} vN−1v_{N-1}

输入从标准输入中按以下格式给出:

NN
P1P_1 P2P_2 …\dots PNP_N
u1u_1 v1v_1
u2u_2 v2v_2
⋮\vdots
uN−1u_{N-1} vN−1v_{N-1}

输出格式

Output NN integers f(1),f(2),…,f(N)f(1), f(2), \dots, f(N), separated by spaces, where each integer represents the risk level of each point.

输出 NN 个整数 f(1),f(2),…,f(N)f(1), f(2), \dots, f(N),以空格分隔,其中每个整数表示对应点的风险等级。

输入输出样例

  • 输入#1

    6
    3 1 2 4 6 5
    1 3
    2 3
    3 4
    4 5
    4 6

    输出#1

    14 14 8 8 14 14

说明/提示

表示言語

/ /

Constraints

  • 3≤N≤100 0003 \le N \le 100\,000
  • 1≤ui<vi≤N1 \le u_i < v_i \le N
  • The given graph is a tree.
  • PP is a permutation.
  • All input values are integers.

表示语言

/ /

限制条件

  • 3≤N≤100 0003 \le N \le 100\,000
  • 1≤ui<vi≤N1 \le u_i < v_i \le N
  • 给定的图是一棵树。
  • PP 是一个排列。
  • 所有输入值均为整数。

输入解题思路,AI测评打分。不知道怎么写?

首页