結果
問題 | No.1813 Magical Stones |
ユーザー | MasKoaTS |
提出日時 | 2024-11-30 21:58:43 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 493 ms / 2,000 ms |
コード長 | 2,554 bytes |
コンパイル時間 | 568 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 139,792 KB |
最終ジャッジ日時 | 2024-11-30 21:58:58 |
合計ジャッジ時間 | 11,995 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 37 ms
52,480 KB |
testcase_01 | AC | 43 ms
52,096 KB |
testcase_02 | AC | 40 ms
52,608 KB |
testcase_03 | AC | 141 ms
102,064 KB |
testcase_04 | AC | 40 ms
52,224 KB |
testcase_05 | AC | 39 ms
52,224 KB |
testcase_06 | AC | 119 ms
78,336 KB |
testcase_07 | AC | 42 ms
53,248 KB |
testcase_08 | AC | 61 ms
64,768 KB |
testcase_09 | AC | 70 ms
70,144 KB |
testcase_10 | AC | 51 ms
60,672 KB |
testcase_11 | AC | 174 ms
91,628 KB |
testcase_12 | AC | 40 ms
52,992 KB |
testcase_13 | AC | 39 ms
52,864 KB |
testcase_14 | AC | 134 ms
101,552 KB |
testcase_15 | AC | 463 ms
139,280 KB |
testcase_16 | AC | 449 ms
138,280 KB |
testcase_17 | AC | 404 ms
139,176 KB |
testcase_18 | AC | 449 ms
136,356 KB |
testcase_19 | AC | 493 ms
139,792 KB |
testcase_20 | AC | 443 ms
137,520 KB |
testcase_21 | AC | 448 ms
135,360 KB |
testcase_22 | AC | 460 ms
136,756 KB |
testcase_23 | AC | 492 ms
137,620 KB |
testcase_24 | AC | 92 ms
76,928 KB |
testcase_25 | AC | 141 ms
83,328 KB |
testcase_26 | AC | 128 ms
78,336 KB |
testcase_27 | AC | 336 ms
117,284 KB |
testcase_28 | AC | 348 ms
127,020 KB |
testcase_29 | AC | 370 ms
124,628 KB |
testcase_30 | AC | 367 ms
121,648 KB |
testcase_31 | AC | 449 ms
134,380 KB |
testcase_32 | AC | 37 ms
52,608 KB |
testcase_33 | AC | 36 ms
52,608 KB |
testcase_34 | AC | 111 ms
78,208 KB |
testcase_35 | AC | 116 ms
81,536 KB |
testcase_36 | AC | 107 ms
77,056 KB |
testcase_37 | AC | 147 ms
88,064 KB |
testcase_38 | AC | 161 ms
95,432 KB |
testcase_39 | AC | 320 ms
120,964 KB |
testcase_40 | AC | 120 ms
78,592 KB |
testcase_41 | AC | 88 ms
77,696 KB |
testcase_42 | AC | 113 ms
82,304 KB |
testcase_43 | AC | 129 ms
80,640 KB |
ソースコード
import sys input = sys.stdin.readline """ 強連結成分分解(SCC) ref : https://manabitimes.jp/math/1250 """ class SCC: def __init__(self, n): self.n = n self.route = [[] for _ in [0] * n] self.rev_route = [[] for _ in [0] * n] self.label = [-1] * n self.count = 0 def add_edge(self, v_first, v_second): self.route[v_first].append(v_second) self.rev_route[v_second].append(v_first) def _build(self): self.v_list = [] self.visited = set([]) for v in range(self.n): if(v in self.visited): continue self._dfs(v) for v in reversed(self.v_list): if(self.label[v] != -1): continue self._rev_dfs(v) self.count += 1 def _dfs(self, v_start): stack = [v_start, 0] while(stack): v, id = stack[-2:] if(id == 0 and v in self.visited): stack.pop() stack.pop() continue self.visited.add(v) if(id < len(self.route[v])): stack[-1] += 1 stack.extend([self.route[v][id], 0]) continue stack.pop() self.v_list.append(stack.pop()) def _rev_dfs(self, v_start): stack = [v_start] self.label[v_start] = self.count while(stack): v_now = stack.pop() for v_next in self.rev_route[v_now]: if(self.label[v_next] != -1): continue self.label[v_next] = self.count stack.append(v_next) def construct(self): self._build() dag = [[] for _ in [0] * self.count] group = [[] for _ in [0] * self.count] for v, lb in enumerate(self.label): for v_next in self.route[v]: lb_next = self.label[v_next] if(lb == lb_next): continue dag[lb].append(lb_next) group[lb].append(v) return dag, group ''' Main Code ''' n, m = map(int, input().split()) edges = [[x - 1 for x in map(int, input().split())] for _ in [0] * m] scc = SCC(n) for x, y in edges: scc.add_edge(x, y) dag, _ = scc.construct() N = len(dag) if(N == 1): exit(print(0)) zero_in = [True] * N zero_out = [True] * N for i in range(N): if(dag[i]): zero_out[i] = False for j in dag[i]: zero_in[j] = False ans = max(sum(zero_in), sum(zero_out)) print(ans)