結果
問題 | No.1976 Cut then Connect |
ユーザー | tktk_snsn |
提出日時 | 2022-06-10 23:59:14 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 158 ms / 2,000 ms |
コード長 | 1,769 bytes |
コンパイル時間 | 154 ms |
コンパイル使用メモリ | 82,536 KB |
実行使用メモリ | 91,404 KB |
最終ジャッジ日時 | 2024-05-03 12:21:47 |
合計ジャッジ時間 | 4,181 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 32 ms
53,164 KB |
testcase_01 | AC | 33 ms
53,616 KB |
testcase_02 | AC | 133 ms
90,736 KB |
testcase_03 | AC | 101 ms
83,580 KB |
testcase_04 | AC | 68 ms
79,360 KB |
testcase_05 | AC | 98 ms
83,208 KB |
testcase_06 | AC | 112 ms
85,004 KB |
testcase_07 | AC | 95 ms
83,352 KB |
testcase_08 | AC | 154 ms
91,404 KB |
testcase_09 | AC | 133 ms
89,368 KB |
testcase_10 | AC | 75 ms
79,908 KB |
testcase_11 | AC | 149 ms
91,056 KB |
testcase_12 | AC | 52 ms
68,816 KB |
testcase_13 | AC | 111 ms
84,784 KB |
testcase_14 | AC | 106 ms
84,288 KB |
testcase_15 | AC | 114 ms
84,364 KB |
testcase_16 | AC | 142 ms
90,256 KB |
testcase_17 | AC | 81 ms
81,472 KB |
testcase_18 | AC | 55 ms
70,112 KB |
testcase_19 | AC | 124 ms
87,788 KB |
testcase_20 | AC | 158 ms
91,124 KB |
testcase_21 | AC | 107 ms
84,080 KB |
testcase_22 | AC | 33 ms
53,200 KB |
testcase_23 | AC | 36 ms
54,564 KB |
testcase_24 | AC | 36 ms
53,216 KB |
testcase_25 | AC | 33 ms
52,836 KB |
testcase_26 | AC | 34 ms
52,864 KB |
testcase_27 | AC | 33 ms
52,464 KB |
testcase_28 | AC | 33 ms
52,464 KB |
testcase_29 | AC | 35 ms
54,060 KB |
testcase_30 | AC | 33 ms
53,588 KB |
testcase_31 | AC | 33 ms
53,324 KB |
testcase_32 | AC | 34 ms
54,356 KB |
ソースコード
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) 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): l = L[i] r = R[i+1] d = max(l, r, (l+1)//2 + (r+1)//2 + 1) ans = min(ans, d) print(ans)