結果
問題 | No.1813 Magical Stones |
ユーザー | MasKoaTS |
提出日時 | 2021-12-13 12:12:36 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 464 ms / 2,000 ms |
コード長 | 2,231 bytes |
コンパイル時間 | 267 ms |
コンパイル使用メモリ | 86,936 KB |
実行使用メモリ | 121,708 KB |
最終ジャッジ日時 | 2023-09-29 14:44:38 |
合計ジャッジ時間 | 13,723 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge14 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 70 ms
71,040 KB |
testcase_01 | AC | 69 ms
71,232 KB |
testcase_02 | AC | 70 ms
71,320 KB |
testcase_03 | AC | 142 ms
94,356 KB |
testcase_04 | AC | 70 ms
71,320 KB |
testcase_05 | AC | 69 ms
71,296 KB |
testcase_06 | AC | 133 ms
78,872 KB |
testcase_07 | AC | 72 ms
71,264 KB |
testcase_08 | AC | 84 ms
76,388 KB |
testcase_09 | AC | 96 ms
77,224 KB |
testcase_10 | AC | 75 ms
75,208 KB |
testcase_11 | AC | 204 ms
85,792 KB |
testcase_12 | AC | 71 ms
71,276 KB |
testcase_13 | AC | 70 ms
71,320 KB |
testcase_14 | AC | 140 ms
94,184 KB |
testcase_15 | AC | 464 ms
119,816 KB |
testcase_16 | AC | 446 ms
121,336 KB |
testcase_17 | AC | 359 ms
113,380 KB |
testcase_18 | AC | 436 ms
120,908 KB |
testcase_19 | AC | 435 ms
119,744 KB |
testcase_20 | AC | 438 ms
121,192 KB |
testcase_21 | AC | 437 ms
121,708 KB |
testcase_22 | AC | 442 ms
121,432 KB |
testcase_23 | AC | 432 ms
120,772 KB |
testcase_24 | AC | 97 ms
77,436 KB |
testcase_25 | AC | 173 ms
83,036 KB |
testcase_26 | AC | 146 ms
80,152 KB |
testcase_27 | AC | 349 ms
111,176 KB |
testcase_28 | AC | 340 ms
101,900 KB |
testcase_29 | AC | 372 ms
108,792 KB |
testcase_30 | AC | 355 ms
102,164 KB |
testcase_31 | AC | 434 ms
112,144 KB |
testcase_32 | AC | 70 ms
71,388 KB |
testcase_33 | AC | 71 ms
71,152 KB |
testcase_34 | AC | 126 ms
78,520 KB |
testcase_35 | AC | 117 ms
78,524 KB |
testcase_36 | AC | 124 ms
79,152 KB |
testcase_37 | AC | 153 ms
82,068 KB |
testcase_38 | AC | 184 ms
94,024 KB |
testcase_39 | AC | 352 ms
104,376 KB |
testcase_40 | AC | 136 ms
79,076 KB |
testcase_41 | AC | 94 ms
76,712 KB |
testcase_42 | AC | 116 ms
78,352 KB |
testcase_43 | AC | 141 ms
80,196 KB |
ソースコード
import sys input = sys.stdin.readline """ Strongly Connected Components source ; https://tjkendev.github.io/procon-library/python/graph/scc.html 注) PyPy3 で提出した際の TLE を防ぐため、dfs 部分を非再帰で実装 """ # 強連結成分分解(SCC): グラフGに対するSCCを行う # 入力: <N>: 頂点サイズ, <G>: 順方向の有向グラフ, <RG>: 逆方向の有向グラフ # 出力: (<ラベル数>, <各頂点のラベル番号>) def scc(N, G, RG): order = [] used = [0]*N group = [None]*N cnt = [0]*N def dfs(s): que = [s] used[s] = 1 while(que): v = que[-1] while(cnt[v] < len(G[v])): t = G[v][cnt[v]] cnt[v] += 1 if not used[t]: used[t] = 1 que.append(t) break else: order.append(que.pop()) def rdfs(s, col): used[s] = 1 que = [s] while(que): v = que.pop() group[v] = col for t in RG[v]: if not used[t]: used[t] = 1 que.append(t) for i in range(N): if not used[i]: dfs(i) used = [0]*N label = 0 for s in reversed(order): if not used[s]: rdfs(s, label) label += 1 return label, group # 縮約後のグラフを構築 def construct(N, G, label, group): G0 = [set() for i in range(label)] GP = [[] for i in range(label)] for v in range(N): lbs = group[v] for w in G[v]: lbt = group[w] if lbs == lbt: continue G0[lbs].add(lbt) GP[lbs].append(v) return G0,GP """ Main Code """ N,M = map(int,input().split()) G = [[] for _ in [0]*N] GR = [[] for _ in [0]*N] for _ in [0]*M: a,b = map(int,input().split()) G[a-1].append(b-1) GR[b-1].append(a-1) label,group = scc(N,G,GR) DAG,_ = construct(N,G,label,group) 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)