結果
問題 | No.1833 Subway Planning |
ユーザー | mkawa2 |
提出日時 | 2022-02-06 22:29:48 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 825 ms / 4,000 ms |
コード長 | 2,965 bytes |
コンパイル時間 | 345 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 208,800 KB |
最終ジャッジ日時 | 2024-06-11 14:32:45 |
合計ジャッジ時間 | 14,833 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
52,992 KB |
testcase_01 | AC | 38 ms
52,992 KB |
testcase_02 | AC | 36 ms
52,864 KB |
testcase_03 | AC | 36 ms
53,248 KB |
testcase_04 | AC | 428 ms
165,184 KB |
testcase_05 | AC | 437 ms
164,112 KB |
testcase_06 | AC | 541 ms
159,900 KB |
testcase_07 | AC | 739 ms
158,068 KB |
testcase_08 | AC | 725 ms
164,464 KB |
testcase_09 | AC | 488 ms
146,932 KB |
testcase_10 | AC | 639 ms
208,800 KB |
testcase_11 | AC | 590 ms
152,556 KB |
testcase_12 | AC | 502 ms
150,900 KB |
testcase_13 | AC | 636 ms
206,580 KB |
testcase_14 | AC | 537 ms
189,008 KB |
testcase_15 | AC | 511 ms
125,440 KB |
testcase_16 | AC | 642 ms
142,724 KB |
testcase_17 | AC | 825 ms
187,348 KB |
testcase_18 | AC | 798 ms
177,932 KB |
testcase_19 | AC | 572 ms
133,164 KB |
testcase_20 | AC | 495 ms
122,736 KB |
testcase_21 | AC | 815 ms
170,984 KB |
testcase_22 | AC | 806 ms
164,940 KB |
testcase_23 | AC | 38 ms
53,120 KB |
testcase_24 | AC | 39 ms
53,376 KB |
testcase_25 | AC | 39 ms
52,864 KB |
ソースコード
import sys # sys.setrecursionlimit(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() # dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] # inf = (1 << 63)-1 inf = (1 << 31)-1 # md = 10**9+7 md = 998244353 from heapq import * class RemovableHeap: def __init__(self): self.hp = [] self.rem = [] self.cnt = {} self.size = 0 self.s = 0 def __len__(self): return self.size def __getitem__(self, i): assert i == 0 self.__trim() return self.hp[0] def push(self, val): heappush(self.hp, val) self.s += val if val in self.cnt: self.cnt[val] += 1 else: self.cnt[val] = 1 self.size += 1 return True def pop(self): self.__trim() assert self.hp self.size -= 1 val = heappop(self.hp) self.s -= val if self.cnt[val] == 1: del self.cnt[val] else: self.cnt[val] -= 1 return val def remove(self, val): if val not in self.cnt: return False self.size -= 1 self.s -= val if self.cnt[val] == 1: del self.cnt[val] else: self.cnt[val] -= 1 heappush(self.rem, val) return True def __trim(self): while self.rem and self.rem[0] == self.hp[0]: heappop(self.hp) heappop(self.rem) def dfs(root): hp = RemovableHeap() # hp.push(inf) st = [root] while st: u = st.pop() for v, c in to[u]: if v == par[u]: continue par[v] = u cc[v] = c mns[v] = min(mns[u], c) hp.push(-c) st.append(v) if len(hp) == 0: return 0 ans = -hp[0] st = [(root, 1)] while st: u, f = st.pop() if f: if u != root: hp.remove(-cc[u]) if len(hp): ans = min(ans, max(-hp[0], mx-mns[u])) else: ans = min(ans, mx-mns[u]) st.append((u, 0)) for v, c in to[u]: if v == par[u]: continue st.append((v, 1)) else: hp.push(-cc[u]) return ans n = II() to = [[] for _ in range(n)] mx = -1 ru = rv = 0 for i in range(1, n): u, v, c = LI1() c += 1 to[u].append((v, c)) to[v].append((u, c)) if c > mx: mx, ru, rv = c, u, v par = [-1]*n cc = [0]*n par[ru] = rv par[rv] = ru mns = [inf]*n ans = max(dfs(ru), dfs(rv)) print(ans)