結果
問題 | No.1194 Replace |
ユーザー | tktk_snsn |
提出日時 | 2021-02-08 22:48:43 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,249 ms / 2,000 ms |
コード長 | 2,896 bytes |
コンパイル時間 | 139 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 282,988 KB |
最終ジャッジ日時 | 2024-07-06 03:24:49 |
合計ジャッジ時間 | 21,954 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 852 ms
225,404 KB |
testcase_01 | AC | 872 ms
229,556 KB |
testcase_02 | AC | 740 ms
201,772 KB |
testcase_03 | AC | 611 ms
177,768 KB |
testcase_04 | AC | 928 ms
231,656 KB |
testcase_05 | AC | 834 ms
222,228 KB |
testcase_06 | AC | 709 ms
209,076 KB |
testcase_07 | AC | 1,241 ms
282,668 KB |
testcase_08 | AC | 1,229 ms
282,988 KB |
testcase_09 | AC | 1,217 ms
282,736 KB |
testcase_10 | AC | 1,216 ms
282,848 KB |
testcase_11 | AC | 1,249 ms
282,684 KB |
testcase_12 | AC | 1,233 ms
282,608 KB |
testcase_13 | AC | 823 ms
208,240 KB |
testcase_14 | AC | 635 ms
168,824 KB |
testcase_15 | AC | 635 ms
159,684 KB |
testcase_16 | AC | 770 ms
173,056 KB |
testcase_17 | AC | 615 ms
160,820 KB |
testcase_18 | AC | 528 ms
154,272 KB |
testcase_19 | AC | 767 ms
199,624 KB |
testcase_20 | AC | 37 ms
52,864 KB |
testcase_21 | AC | 35 ms
52,608 KB |
testcase_22 | AC | 33 ms
53,012 KB |
testcase_23 | AC | 337 ms
121,088 KB |
testcase_24 | AC | 175 ms
93,440 KB |
testcase_25 | AC | 109 ms
81,024 KB |
testcase_26 | AC | 347 ms
116,224 KB |
testcase_27 | AC | 112 ms
82,816 KB |
testcase_28 | AC | 214 ms
94,592 KB |
testcase_29 | AC | 104 ms
78,848 KB |
ソースコード
import sys input = sys.stdin.buffer.readline sys.setrecursionlimit(10 ** 7) class SCC_graph(object): def __init__(self, n): """n:ノード数""" self.n = n self.edges = [] def add_edge(self, frm, to): """frm -> toへ有効辺を張る""" self.edges.append((frm, to)) def __csr(self): self.start = [0] * (self.n + 1) self.elist = [0] * len(self.edges) for frm, to in self.edges: self.start[frm + 1] += 1 for i in range(1, self.n + 1): self.start[i] += self.start[i - 1] cnt = self.start[:] for frm, to in self.edges: self.elist[cnt[frm]] = to cnt[frm] += 1 def __dfs(self, v): self.low[v] = self.now_ord self.order[v] = self.now_ord self.now_ord += 1 self.visited.append(v) for i in range(self.start[v], self.start[v + 1]): to = self.elist[i] if self.order[to] == -1: self.__dfs(to) self.low[v] = min(self.low[v], self.low[to]) else: self.low[v] = min(self.low[v], self.order[to]) if self.low[v] == self.order[v]: while self.visited: u = self.visited.pop() self.order[u] = self.n self.ids[u] = self.group_num if u == v: break self.group_num += 1 def _make_scc_ids(self): self.__csr() self.now_ord = 0 self.group_num = 0 self.visited = [] self.low = [0] * self.n self.ids = [0] * self.n self.order = [-1] * self.n for i in range(self.n): if self.order[i] == -1: self.__dfs(i) for i in range(self.n): self.ids[i] = self.group_num - 1 - self.ids[i] def scc(self): self._make_scc_ids() groups = [[] for _ in range(self.group_num)] for i in range(self.n): groups[self.ids[i]].append(i) return groups N, M = map(int, input().split()) BC = tuple(tuple(map(int, input().split())) for _ in range(M)) val = set() for b, c in BC: val.add(b) val.add(c) val = sorted(val) vtoi = {v: i for i, v in enumerate(val)} sz = len(val) G = SCC_graph(sz) for b, c in BC: s = vtoi[b] t = vtoi[c] G.add_edge(s, t) group = G.scc() gtoi = dict() for i, g in enumerate(group): for x in g: gtoi[x] = i edge = [set() for _ in range(len(group))] for b, c in BC: s = gtoi[vtoi[b]] t = gtoi[vtoi[c]] edge[t].add(s) vmax = [0] * len(group) for s in reversed(range(len(group))): g = group[s] vmax[s] = max(vmax[s], max(g)) for t in edge[s]: vmax[t] = max(vmax[t], vmax[s]) ans = N * (N + 1) // 2 for v, g in zip(vmax, group): for i in g: ans -= val[i] ans += val[v] print(ans)