結果
問題 | No.1976 Cut then Connect |
ユーザー | tktk_snsn |
提出日時 | 2022-06-10 23:59:14 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 168 ms / 2,000 ms |
コード長 | 1,769 bytes |
コンパイル時間 | 299 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 91,364 KB |
最終ジャッジ日時 | 2024-11-24 12:16:54 |
合計ジャッジ時間 | 3,974 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 36 ms
52,480 KB |
testcase_01 | AC | 40 ms
52,608 KB |
testcase_02 | AC | 146 ms
90,504 KB |
testcase_03 | AC | 109 ms
83,412 KB |
testcase_04 | AC | 71 ms
78,976 KB |
testcase_05 | AC | 99 ms
82,936 KB |
testcase_06 | AC | 115 ms
85,112 KB |
testcase_07 | AC | 96 ms
83,072 KB |
testcase_08 | AC | 157 ms
91,264 KB |
testcase_09 | AC | 145 ms
89,148 KB |
testcase_10 | AC | 79 ms
80,100 KB |
testcase_11 | AC | 168 ms
91,364 KB |
testcase_12 | AC | 55 ms
66,944 KB |
testcase_13 | AC | 121 ms
84,736 KB |
testcase_14 | AC | 114 ms
84,608 KB |
testcase_15 | AC | 113 ms
84,096 KB |
testcase_16 | AC | 146 ms
89,856 KB |
testcase_17 | AC | 83 ms
81,280 KB |
testcase_18 | AC | 57 ms
69,248 KB |
testcase_19 | AC | 137 ms
87,168 KB |
testcase_20 | AC | 161 ms
91,260 KB |
testcase_21 | AC | 111 ms
83,924 KB |
testcase_22 | AC | 36 ms
52,224 KB |
testcase_23 | AC | 36 ms
52,480 KB |
testcase_24 | AC | 36 ms
52,864 KB |
testcase_25 | AC | 37 ms
52,224 KB |
testcase_26 | AC | 35 ms
52,480 KB |
testcase_27 | AC | 37 ms
52,096 KB |
testcase_28 | AC | 37 ms
52,352 KB |
testcase_29 | AC | 36 ms
52,480 KB |
testcase_30 | AC | 36 ms
52,480 KB |
testcase_31 | AC | 36 ms
52,224 KB |
testcase_32 | AC | 35 ms
52,736 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)