結果

問題 No.1718 Random Squirrel
ユーザー mkawa2mkawa2
提出日時 2023-12-26 21:59:03
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,515 bytes
コンパイル時間 252 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 113,136 KB
最終ジャッジ日時 2023-12-26 21:59:12
合計ジャッジ時間 9,036 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,460 KB
testcase_01 AC 36 ms
53,460 KB
testcase_02 AC 40 ms
53,460 KB
testcase_03 AC 35 ms
53,460 KB
testcase_04 AC 36 ms
53,460 KB
testcase_05 AC 35 ms
53,460 KB
testcase_06 AC 36 ms
53,460 KB
testcase_07 AC 36 ms
53,460 KB
testcase_08 AC 37 ms
53,460 KB
testcase_09 AC 36 ms
53,460 KB
testcase_10 AC 36 ms
53,460 KB
testcase_11 WA -
testcase_12 AC 130 ms
79,868 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 231 ms
94,888 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 291 ms
109,624 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 323 ms
112,992 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 317 ms
113,136 KB
testcase_26 AC 191 ms
106,996 KB
testcase_27 WA -
testcase_28 AC 187 ms
106,996 KB
testcase_29 AC 213 ms
108,356 KB
testcase_30 AC 198 ms
108,412 KB
testcase_31 AC 187 ms
108,212 KB
testcase_32 AC 191 ms
108,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(1000005)
# sys.set_int_max_str_digits(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
# inf = -1-(-1 << 31)
inf = -1-(-1 << 62)

# md = 10**9+7
md = 998244353


def rerooting(root=0):
    stack = [root]
    while stack:
        u = stack.pop()
        uu.append(u)
        vv = []
        # !!!check the format of the "to"!!!
        for v in to[u]:
            if v == par[u]: continue
            vv.append(v)
            par[v] = u
            stack.append(v)
            rdp[v] = dp[u]
        to[u] = vv

    # bottom up
    for u in uu[::-1]:
        p = par[u]
        if p != -1:
            trans_up(u)
            dp[p] = op(dp[p], dp[u])

    # top down
    for u in uu:
        cs = e()
        # !!!check the format of the "to"!!!
        for v in to[u]:
            rdp[v] = op(rdp[v], cs)
            cs = op(cs, dp[v])
        cs = rdp[u]
        for v in to[u][::-1]:
            rdp[v] = op(rdp[v], cs)
            trans_down(v)
            cs = op(cs, dp[v])
        dp[u] = cs

def op(a, b):
    return max(a,b)

# u to parent[u]
def trans_up(u):
    if dp[u]>-1:dp[u]+=1
    return

# parent[u] to u
def trans_down(u):
    if rdp[u]>-1:rdp[u]+=1
    return

n,k=LI()
to=[[] for _ in range(n)]
for _ in range(n-1):
    u,v=LI1()
    to[u].append(v)
    to[v].append(u)
dd=LI1()

e = lambda: -1
par = [-1]*n

# dp初期化
dp = [e() for _ in range(n)]
rdp = [e() for _ in range(n)]
for d in dd:dp[d]=rdp[d]=0
uu = []

rerooting(dd[0])
# print(uu)
# print(dp)

near=[-1]*n
dist=[0]*n
for d in dd:near[d]=d

for u in uu:
    if near[u]==-1:
        p = par[u]
        dist[u]=dist[p]+1
        near[u]=near[p]

inside=[0]*n
for d in dd:inside[d]=1
for u in uu[:0:-1]:
    p=par[u]
    inside[p]|=inside[u]

ans=[(sum(inside)-1)*2]*n
for u in range(n):
    if inside[u]:ans[u]-=dp[u]
for u in range(n):
    if inside[u]:continue
    v=near[u]
    ans[u]=ans[v]+dist[u]

print(*ans,sep="\n")
0