結果

問題 No.1718 Random Squirrel
ユーザー puznekopuzneko
提出日時 2021-11-15 23:46:27
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,739 bytes
コンパイル時間 2,114 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 137,020 KB
最終ジャッジ日時 2024-05-09 00:24:37
合計ジャッジ時間 11,585 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin
n, m, *indata = map(int, stdin.read().split())
offset = 0
g = [[] for i in range(n+1)]
for i in range(n-1):
    s, t = indata[offset + 2*i],indata[offset + 2*i+1]
    g[s].append((t,i))
    g[t].append((s,i))

offset += (n-1)*2
d = [False for i in range(n+1)]
for i in range(m):
    d[indata[offset + i]] = True

que = [(1,0)]
dp = [-1 for i in range(n+1)]
dpdist = [-1 for i in range(n+1)]
check = [0 for i in range(n+1)]
while que:
    now, inout = que.pop()
    if inout == 0:
        check[now] = 1
        que.append((now,1))
        for i, ind in g[now]:
            if check[i] == 0:
                que.append((i,0))
    else:
        kari = -1
        distmax = -1
        check[now] = 2
        for i, ind in g[now]:
            if check[i] == 2:
                if dp[i] != -1:
                    kari += dp[i] + 2
                distmax = max(distmax,dpdist[i])
        if kari != -1:
            kari += 1
            distmax += 1
        else:
            if d[now]:
                kari = 0
                distmax = max(distmax,0)
        dp[now] = kari
        dpdist[now] = distmax

dp2 = [-1 for i in range(n+1)]
dpdist2 = [-1 for i in range(n+1)]
que = [(1,-1)]
check = [False for i in range(n+1)]
while que:
    now, come = que.pop()
    check[now] = True
    ruiseki = [0]
    ruisekidist = [-1]
    for i, ind in g[now]:
        kari = ruiseki[-1]
        distmax = ruisekidist[-1]
        if ind != come:
            if dp[i] != -1:
                kari = kari + dp[i] + 2
                distmax = max(distmax,dpdist[i])
        else:
            if dp2[ind] != -1:
                kari = kari + dp2[ind] + 2
                distmax = max(distmax,dpdist2[ind])
        ruiseki.append(kari)
        ruisekidist.append(distmax)
    ruiseki2 = [0]
    ruisekidist2 = [-1]
    for i, ind in reversed(g[now]):
        kari = ruiseki2[-1]
        distmax = ruisekidist2[-1]
        if ind != come:
            if dp[i] != -1:
                kari = kari + dp[i] + 2
                distmax = max(distmax,dpdist[i])
        else:
            if dp2[ind] != -1:
                kari = kari + dp2[ind] + 2
                distmax = max(distmax,dpdist2[ind])
        ruiseki2.append(kari)
        ruisekidist2.append(distmax)
    dp[now] = ruiseki[-1]
    dpdist[now] = ruisekidist[-1] + 1
    val = 0
    for i, ind in g[now]:
        kari = ruiseki[val] + ruiseki2[-2-val]
        distmax = max(ruisekidist[val], ruisekidist2[-2-val])
        dp2[ind] = kari
        dpdist2[ind] = distmax + 1
        val += 1
    for i, ind in g[now]:
        if not check[i]:
            que.append((i, ind))

for i in range(1,n+1):
    ans = dp[i] - dpdist[i]
    print("{}".format(ans))
print(dp)
print(dpdist)
0