結果
問題 | No.1813 Magical Stones |
ユーザー | MasKoaTS |
提出日時 | 2021-10-24 21:31:13 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 1,058 ms / 2,000 ms |
コード長 | 2,231 bytes |
コンパイル時間 | 568 ms |
コンパイル使用メモリ | 11,116 KB |
実行使用メモリ | 64,412 KB |
最終ジャッジ日時 | 2023-09-23 07:08:26 |
合計ジャッジ時間 | 19,249 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 17 ms
8,032 KB |
testcase_01 | AC | 17 ms
8,028 KB |
testcase_02 | AC | 17 ms
8,536 KB |
testcase_03 | AC | 467 ms
64,220 KB |
testcase_04 | AC | 17 ms
8,108 KB |
testcase_05 | AC | 17 ms
8,176 KB |
testcase_06 | AC | 34 ms
10,940 KB |
testcase_07 | AC | 18 ms
8,496 KB |
testcase_08 | AC | 20 ms
8,908 KB |
testcase_09 | AC | 23 ms
9,340 KB |
testcase_10 | AC | 19 ms
8,832 KB |
testcase_11 | AC | 213 ms
26,540 KB |
testcase_12 | AC | 17 ms
8,064 KB |
testcase_13 | AC | 17 ms
8,068 KB |
testcase_14 | AC | 459 ms
64,412 KB |
testcase_15 | AC | 1,032 ms
60,788 KB |
testcase_16 | AC | 1,048 ms
60,780 KB |
testcase_17 | AC | 943 ms
48,996 KB |
testcase_18 | AC | 1,053 ms
60,820 KB |
testcase_19 | AC | 1,042 ms
60,880 KB |
testcase_20 | AC | 1,042 ms
60,884 KB |
testcase_21 | AC | 1,058 ms
60,836 KB |
testcase_22 | AC | 1,046 ms
60,668 KB |
testcase_23 | AC | 1,036 ms
60,884 KB |
testcase_24 | AC | 23 ms
9,072 KB |
testcase_25 | AC | 122 ms
19,708 KB |
testcase_26 | AC | 46 ms
12,104 KB |
testcase_27 | AC | 731 ms
48,896 KB |
testcase_28 | AC | 784 ms
39,688 KB |
testcase_29 | AC | 831 ms
49,212 KB |
testcase_30 | AC | 796 ms
45,840 KB |
testcase_31 | AC | 985 ms
59,748 KB |
testcase_32 | AC | 16 ms
8,536 KB |
testcase_33 | AC | 16 ms
8,540 KB |
testcase_34 | AC | 38 ms
10,024 KB |
testcase_35 | AC | 94 ms
11,888 KB |
testcase_36 | AC | 33 ms
11,064 KB |
testcase_37 | AC | 206 ms
16,364 KB |
testcase_38 | AC | 317 ms
44,424 KB |
testcase_39 | AC | 708 ms
51,884 KB |
testcase_40 | AC | 35 ms
10,544 KB |
testcase_41 | AC | 36 ms
9,340 KB |
testcase_42 | AC | 96 ms
11,904 KB |
testcase_43 | AC | 63 ms
11,604 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]: source[i] = sink[j] = False ans = max(sum(source),sum(sink)) print(ans)