結果

問題 No.1718 Random Squirrel
ユーザー mkawa2mkawa2
提出日時 2023-12-26 22:08:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 320 ms / 2,000 ms
コード長 2,534 bytes
コンパイル時間 215 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 113,136 KB
最終ジャッジ日時 2023-12-26 22:08:53
合計ジャッジ時間 7,048 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,460 KB
testcase_01 AC 35 ms
53,456 KB
testcase_02 AC 34 ms
53,460 KB
testcase_03 AC 36 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 34 ms
53,460 KB
testcase_08 AC 34 ms
53,460 KB
testcase_09 AC 34 ms
53,460 KB
testcase_10 AC 36 ms
53,460 KB
testcase_11 AC 230 ms
96,488 KB
testcase_12 AC 124 ms
79,868 KB
testcase_13 AC 185 ms
88,912 KB
testcase_14 AC 224 ms
96,180 KB
testcase_15 AC 214 ms
94,372 KB
testcase_16 AC 171 ms
85,680 KB
testcase_17 AC 191 ms
94,780 KB
testcase_18 AC 249 ms
109,520 KB
testcase_19 AC 204 ms
93,124 KB
testcase_20 AC 135 ms
83,404 KB
testcase_21 AC 249 ms
104,144 KB
testcase_22 AC 320 ms
113,000 KB
testcase_23 AC 284 ms
104,420 KB
testcase_24 AC 264 ms
104,328 KB
testcase_25 AC 287 ms
113,136 KB
testcase_26 AC 186 ms
106,996 KB
testcase_27 AC 185 ms
107,888 KB
testcase_28 AC 184 ms
106,996 KB
testcase_29 AC 174 ms
106,992 KB
testcase_30 AC 192 ms
107,648 KB
testcase_31 AC 184 ms
107,376 KB
testcase_32 AC 177 ms
106,992 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
for d in dd:near[d]=d
for u in uu[:0:-1]:
    if near[u]==u:
        p=par[u]
        near[p]=p

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

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

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

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