結果

問題 No.1718 Random Squirrel
ユーザー mkawa2mkawa2
提出日時 2023-12-26 21:59:03
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,515 bytes
コンパイル時間 365 ms
コンパイル使用メモリ 82,580 KB
実行使用メモリ 114,000 KB
最終ジャッジ日時 2024-09-27 15:17:17
合計ジャッジ時間 9,055 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,248 KB
testcase_01 AC 44 ms
52,944 KB
testcase_02 AC 42 ms
52,740 KB
testcase_03 AC 40 ms
52,968 KB
testcase_04 AC 40 ms
53,052 KB
testcase_05 AC 40 ms
52,748 KB
testcase_06 AC 39 ms
52,492 KB
testcase_07 AC 40 ms
53,684 KB
testcase_08 AC 40 ms
54,372 KB
testcase_09 AC 41 ms
53,840 KB
testcase_10 AC 40 ms
53,444 KB
testcase_11 WA -
testcase_12 AC 145 ms
80,336 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 271 ms
95,164 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 342 ms
110,032 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 389 ms
113,704 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 384 ms
114,000 KB
testcase_26 AC 207 ms
107,496 KB
testcase_27 WA -
testcase_28 AC 200 ms
107,200 KB
testcase_29 AC 209 ms
109,284 KB
testcase_30 AC 219 ms
108,712 KB
testcase_31 AC 204 ms
109,148 KB
testcase_32 AC 207 ms
108,780 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