結果

問題 No.1976 Cut then Connect
ユーザー tamatotamato
提出日時 2022-06-10 23:09:28
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 3,785 bytes
コンパイル時間 176 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 138,668 KB
最終ジャッジ日時 2024-09-21 06:48:46
合計ジャッジ時間 7,689 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
54,272 KB
testcase_01 RE -
testcase_02 RE -
testcase_03 AC 273 ms
103,884 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 WA -
testcase_07 RE -
testcase_08 RE -
testcase_09 AC 368 ms
123,800 KB
testcase_10 RE -
testcase_11 WA -
testcase_12 WA -
testcase_13 RE -
testcase_14 AC 326 ms
111,220 KB
testcase_15 RE -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 RE -
testcase_20 AC 447 ms
138,668 KB
testcase_21 AC 290 ms
111,180 KB
testcase_22 RE -
testcase_23 AC 42 ms
53,888 KB
testcase_24 AC 42 ms
54,272 KB
testcase_25 AC 42 ms
54,016 KB
testcase_26 AC 42 ms
54,528 KB
testcase_27 AC 44 ms
54,528 KB
testcase_28 RE -
testcase_29 AC 43 ms
54,912 KB
testcase_30 RE -
testcase_31 RE -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 998244353


def main():
    import sys
    from collections import deque
    input = sys.stdin.readline

    def radius(v0, v1):
        que = deque()
        que.append(v0)
        seen = {}
        seen[v0] = 0
        v_last = 0
        while que:
            v = que.popleft()
            for u in adj[v]:
                if u == v1:
                    continue
                if u not in seen:
                    seen[u] = seen[v] + 1
                    que.append(u)
            v_last = v
        v0 = v_last
        que = deque()
        que.append(v0)
        seen = {}
        seen[v0] = 0
        v_last = 0
        while que:
            v = que.popleft()
            for u in adj[v]:
                if u == v1:
                    continue
                if u not in seen:
                    seen[u] = seen[v] + 1
                    que.append(u)
            v_last = v
        return seen[v_last]

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

    que = deque()
    que.append(1)
    seen = [-1] * (N+1)
    seen[1] = 0
    par = [0] * (N+1)
    child = [[] for _ in range(N+1)]
    seq = []
    while que:
        v = que.popleft()
        seq.append(v)
        for u in adj[v]:
            if seen[u] == -1:
                seen[u] = seen[v] + 1
                par[u] = v
                child[v].append(u)
                que.append(u)
    seq.reverse()

    v0 = seq[0]
    que = deque()
    que.append(v0)
    seen = [-1] * (N+1)
    seen[v0] = 0
    par = [0] * (N+1)
    child = [[] for _ in range(N+1)]
    seq = []
    while que:
        v = que.popleft()
        seq.append(v)
        for u in adj[v]:
            if seen[u] == -1:
                seen[u] = seen[v] + 1
                par[u] = v
                child[v].append(u)
                que.append(u)
    seq.reverse()

    v1 = seq[0]
    D = seen[v1]
    if D & 1:
        v = v1
        for _ in range(D // 2):
            v = par[v]
        vl = v
        vr = par[v]

        dl = radius(vl, vr)
        dr = radius(vr, vl)

        print(max((dl + 1) // 2 + (dr + 1) // 2 + 1, dl, dr))
        assert False
    else:
        ans = N * 2
        v = v1
        for _ in range(D // 2):
            v = par[v]
        v0 = v
        que = deque()
        que.append(v0)
        seen = [-1] * (N+1)
        seen[v0] = 0
        par = [0] * (N+1)
        child = [[] for _ in range(N+1)]
        seq = []
        while que:
            v = que.popleft()
            seq.append(v)
            for u in adj[v]:
                if seen[u] == -1:
                    seen[u] = seen[v] + 1
                    par[u] = v
                    child[v].append(u)
                    que.append(u)
        seq.reverse()

        subtree_len = [(0, 0)]
        for c in child[v0]:
            que = deque()
            seq = []
            que.append(c)
            while que:
                v = que.popleft()
                for vv in child[v]:
                    que.append(vv)
                seq.append(v)
            subtree_len.append((seen[seq[-1]], c))
        subtree_len.sort(key=lambda x: x[0], reverse=True)

        for c in child[v0]:
            dc = radius(c, v0)

            if c == subtree_len[0][1]:
                dc2 = subtree_len[1][0] + subtree_len[2][0]

            elif c == subtree_len[1][1]:
                dc2 = subtree_len[0][0] + subtree_len[2][0]
            else:
                dc2 = subtree_len[0][0] + subtree_len[1][0]
            ans = min(ans, max((dc + 1) // 2 + (dc2 + 1) // 2 + 1, dc, dc2))
            #print(cc, ans, dc, dc2)
        print(ans)
        #print(subtree_len)


if __name__ == '__main__':
    main()
0