結果
問題 | No.1976 Cut then Connect |
ユーザー | tktk_snsn |
提出日時 | 2022-06-10 23:28:33 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,726 bytes |
コンパイル時間 | 151 ms |
コンパイル使用メモリ | 82,440 KB |
実行使用メモリ | 91,676 KB |
最終ジャッジ日時 | 2024-09-21 06:57:13 |
合計ジャッジ時間 | 4,577 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 40 ms
52,224 KB |
testcase_01 | AC | 38 ms
51,968 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 | 38 ms
52,096 KB |
testcase_25 | AC | 38 ms
52,352 KB |
testcase_26 | AC | 39 ms
52,480 KB |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | AC | 38 ms
52,480 KB |
testcase_31 | WA | - |
testcase_32 | AC | 38 ms
52,480 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 + 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)