結果
問題 | No.1813 Magical Stones |
ユーザー | MasKoaTS |
提出日時 | 2021-12-22 14:01:09 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 478 ms / 2,000 ms |
コード長 | 2,280 bytes |
コンパイル時間 | 174 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 111,712 KB |
最終ジャッジ日時 | 2024-09-16 08:36:30 |
合計ジャッジ時間 | 11,485 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge6 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
52,736 KB |
testcase_01 | AC | 40 ms
52,480 KB |
testcase_02 | AC | 37 ms
52,736 KB |
testcase_03 | AC | 110 ms
90,320 KB |
testcase_04 | AC | 37 ms
52,480 KB |
testcase_05 | AC | 37 ms
52,480 KB |
testcase_06 | AC | 105 ms
77,812 KB |
testcase_07 | AC | 40 ms
53,248 KB |
testcase_08 | AC | 54 ms
64,768 KB |
testcase_09 | AC | 60 ms
68,992 KB |
testcase_10 | AC | 46 ms
60,288 KB |
testcase_11 | AC | 158 ms
83,584 KB |
testcase_12 | AC | 40 ms
52,992 KB |
testcase_13 | AC | 40 ms
52,864 KB |
testcase_14 | AC | 110 ms
90,320 KB |
testcase_15 | AC | 464 ms
110,680 KB |
testcase_16 | AC | 455 ms
110,752 KB |
testcase_17 | AC | 458 ms
110,464 KB |
testcase_18 | AC | 457 ms
110,048 KB |
testcase_19 | AC | 478 ms
111,064 KB |
testcase_20 | AC | 452 ms
110,548 KB |
testcase_21 | AC | 466 ms
110,468 KB |
testcase_22 | AC | 456 ms
110,052 KB |
testcase_23 | AC | 452 ms
110,500 KB |
testcase_24 | AC | 77 ms
75,520 KB |
testcase_25 | AC | 133 ms
80,792 KB |
testcase_26 | AC | 119 ms
79,488 KB |
testcase_27 | AC | 326 ms
100,596 KB |
testcase_28 | AC | 356 ms
100,224 KB |
testcase_29 | AC | 356 ms
101,760 KB |
testcase_30 | AC | 354 ms
100,580 KB |
testcase_31 | AC | 429 ms
111,712 KB |
testcase_32 | AC | 37 ms
52,812 KB |
testcase_33 | AC | 39 ms
52,736 KB |
testcase_34 | AC | 103 ms
77,184 KB |
testcase_35 | AC | 93 ms
77,696 KB |
testcase_36 | AC | 99 ms
77,064 KB |
testcase_37 | AC | 133 ms
81,024 KB |
testcase_38 | AC | 149 ms
87,484 KB |
testcase_39 | AC | 308 ms
99,584 KB |
testcase_40 | AC | 111 ms
78,208 KB |
testcase_41 | AC | 78 ms
76,416 KB |
testcase_42 | AC | 97 ms
77,604 KB |
testcase_43 | AC | 122 ms
78,936 KB |
ソースコード
import sys input = sys.stdin.readline """ 強連結成分分解(SCC) from : https://github.com/ryusuke920/kyopro_educational_90_python/blob/main/solve_python/021.py ref : https://manabitimes.jp/math/1250 """ class SCC: def __init__(self, n): self.n = n self.graph = [[] for _ in range(n)] self.rev_graph = [[] for _ in range(n)] self.labels = [-1] * n self.lb_cnt = 0 #グラフに有向辺を追加(逆向きのものを含む) def add_edge(self, v, nxt_v): self.graph[v].append(nxt_v) self.rev_graph[nxt_v].append(v) #SCCグラフの構成 def build(self): self.post_order = [] self.used = [False] * self.n for v in range(self.n): if(not self.used[v]): self._dfs(v) for v in reversed(self.post_order): if(self.labels[v] == -1): self._rev_dfs(v) self.lb_cnt += 1 #SCCグラフを作る上でのDFS def _dfs(self, v): stack = [v, 0] while(stack): v, idx = stack[-2:] if not idx and self.used[v]: stack.pop() stack.pop() else: self.used[v] = True if idx < len(self.graph[v]): stack[-1] += 1 stack.append(self.graph[v][idx]) stack.append(0) else: stack.pop() self.post_order.append(stack.pop()) #逆向きDFS def _rev_dfs(self, v): stack = [v] self.labels[v] = self.lb_cnt while stack: v = stack.pop() for nxt_v in self.rev_graph[v]: if self.labels[nxt_v] != -1: continue stack.append(nxt_v) self.labels[nxt_v] = self.lb_cnt #構成し終えたSCCグラフを連結成分ごとにリストにする def construct(self): self.dag = [[] for i in range(self.lb_cnt)] self.groups = [[] for i in range(self.lb_cnt)] for v, lb in enumerate(self.labels): for nxt_v in self.graph[v]: nxt_lb = self.labels[nxt_v] if lb == nxt_lb: continue self.dag[lb].append(nxt_lb) self.groups[lb].append(v) return self.dag, self.groups """ Main Code """ n,m = map(int, input().split()) scc = SCC(n) for _ in [0] * m: a,b = map(int, input().split()) a -= 1; b -= 1 scc.add_edge(a,b) scc.build() dag,_ = scc.construct() t = len(dag) if(t == 1): print(0) exit(0) source = [True] * t sink = [True] * t for i in range(t): for j in dag[i]: sink[i] = source[j] = False ans = max(sum(source),sum(sink)) print(ans)