結果

問題 No.1976 Cut then Connect
ユーザー tktk_snsntktk_snsn
提出日時 2022-06-10 23:28:33
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,726 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 81,804 KB
実行使用メモリ 91,156 KB
最終ジャッジ日時 2023-10-21 05:48:12
合計ジャッジ時間 5,472 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,468 KB
testcase_01 AC 38 ms
53,468 KB
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 AC 39 ms
53,468 KB
testcase_25 AC 39 ms
53,468 KB
testcase_26 AC 39 ms
53,468 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 39 ms
53,468 KB
testcase_31 WA -
testcase_32 AC 39 ms
53,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 9


def dfs(N, root, G):
    que = [root]
    dist = [-1] * N
    par = [-1] * N
    dist[root] = 0
    while que:
        s = que.pop()
        for t in G[s]:
            if t == par[s]:
                continue
            par[t] = s
            dist[t] = dist[s] + 1
            que.append(t)
    return dist, par


def search(N, path, G):
    M = len(path)
    res = [0] * M
    d = [-1] * N
    for i in range(M):
        ng = set()
        if i:
            ng.add(path[i-1])
        if i < M - 1:
            ng.add(path[i+1])
        que = [path[i]]
        d[path[i]] = 0
        while que:
            s = que.pop()
            for t in G[s]:
                if t in ng:
                    continue
                if d[t] == -1:
                    d[t] = d[s] + 1
                    que.append(t)
                    res[i] = max(res[i], d[t])
    return res


def calc(path, branch):
    M = len(path)
    res = []
    d = 0
    for i in range(M):
        d = max(d, i + branch[i])
        res.append((d + 1) // 2)
    return res


N = int(input())
G = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)

r0 = 0
d0, p0 = dfs(N, r0, G)

r1 = d0.index(max(d0))
d1, p1 = dfs(N, r1, G)

r2 = d1.index(max(d1))

path = [r2]
while 1:
    x = p1[path[-1]]
    if x == -1:
        break
    path.append(x)

M = len(path)
dep = [min(i, M - i - 1) for i in range(M)]
branch = search(N, path, G)


L = calc(path, branch)
R = calc(path[::-1], branch[::-1])[::-1]
ans = inf
for i in range(M - 1):
    ans = min(ans, L[i] + R[i+1] + 1)
print(ans)
0