結果
問題 | No.1813 Magical Stones |
ユーザー | tamato |
提出日時 | 2022-01-14 23:07:55 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 564 ms / 2,000 ms |
コード長 | 3,227 bytes |
コンパイル時間 | 164 ms |
コンパイル使用メモリ | 82,296 KB |
実行使用メモリ | 111,784 KB |
最終ジャッジ日時 | 2024-11-20 13:54:40 |
合計ジャッジ時間 | 11,986 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
55,332 KB |
testcase_01 | AC | 44 ms
55,120 KB |
testcase_02 | AC | 43 ms
55,396 KB |
testcase_03 | AC | 107 ms
97,268 KB |
testcase_04 | AC | 43 ms
54,564 KB |
testcase_05 | AC | 42 ms
55,048 KB |
testcase_06 | AC | 136 ms
78,964 KB |
testcase_07 | AC | 45 ms
55,304 KB |
testcase_08 | AC | 65 ms
71,196 KB |
testcase_09 | AC | 73 ms
74,788 KB |
testcase_10 | AC | 54 ms
65,580 KB |
testcase_11 | AC | 222 ms
89,548 KB |
testcase_12 | AC | 43 ms
54,920 KB |
testcase_13 | AC | 43 ms
55,760 KB |
testcase_14 | AC | 107 ms
97,056 KB |
testcase_15 | AC | 554 ms
110,788 KB |
testcase_16 | AC | 542 ms
111,680 KB |
testcase_17 | AC | 463 ms
104,760 KB |
testcase_18 | AC | 544 ms
111,520 KB |
testcase_19 | AC | 551 ms
111,300 KB |
testcase_20 | AC | 564 ms
111,548 KB |
testcase_21 | AC | 557 ms
111,224 KB |
testcase_22 | AC | 543 ms
111,784 KB |
testcase_23 | AC | 538 ms
111,624 KB |
testcase_24 | AC | 71 ms
73,860 KB |
testcase_25 | AC | 178 ms
84,600 KB |
testcase_26 | AC | 146 ms
80,116 KB |
testcase_27 | AC | 416 ms
104,260 KB |
testcase_28 | AC | 408 ms
97,320 KB |
testcase_29 | AC | 438 ms
102,984 KB |
testcase_30 | AC | 425 ms
100,936 KB |
testcase_31 | AC | 516 ms
110,348 KB |
testcase_32 | AC | 42 ms
55,132 KB |
testcase_33 | AC | 43 ms
55,564 KB |
testcase_34 | AC | 97 ms
78,032 KB |
testcase_35 | AC | 98 ms
77,300 KB |
testcase_36 | AC | 120 ms
78,860 KB |
testcase_37 | AC | 128 ms
80,328 KB |
testcase_38 | AC | 186 ms
93,980 KB |
testcase_39 | AC | 418 ms
105,668 KB |
testcase_40 | AC | 133 ms
79,116 KB |
testcase_41 | AC | 72 ms
74,604 KB |
testcase_42 | AC | 96 ms
77,364 KB |
testcase_43 | AC | 133 ms
79,504 KB |
ソースコード
mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.readline from collections import deque # compressed[v]: 縮約後のグラフで縮約前のvが属する頂点 # num: 縮約後のグラフの頂点数 # 縮約後のグラフの頂点番号はトポロジカル順 def SCC(adj, adj_rev): N = len(adj) - 1 seen = [0] * (N + 1) compressed = [0] * (N + 1) order = [] for v0 in range(1, N + 1): if seen[v0]: continue st = deque() st.append(v0) while st: v = st.pop() if v < 0: order.append(-v) else: if seen[v]: continue seen[v] = 1 st.append(-v) for u in adj[v]: st.append(u) seen = [0] * (N + 1) num = 0 for v0 in reversed(order): if seen[v0]: continue num += 1 st = deque() st.append(v0) seen[v0] = 1 compressed[v0] = num while st: v = st.pop() for u in adj_rev[v]: if seen[u]: continue seen[u] = 1 compressed[u] = num st.append(u) return num, compressed # 縮約後のグラフを構築 # 先にSCC()を実行してnum, compressedを作っておく def construct(adj, num, compressed): N = len(adj) - 1 adj_compressed = [set() for _ in range(num + 1)] for v in range(1, N + 1): v_cmp = compressed[v] for u in adj[v]: u_cmp = compressed[u] if v_cmp != u_cmp: adj_compressed[v_cmp].add(u_cmp) return adj_compressed N, M = map(int, input().split()) adj = [[] for _ in range(N+1)] adj_rev = [[] for _ in range(N + 1)] for _ in range(M): a, b = map(int, input().split()) adj[a].append(b) adj_rev[b].append(a) NN, compressed = SCC(adj, adj_rev) adj_new = construct(adj, NN, compressed) in_num = [0] * (NN + 1) out_num = [0] * (NN + 1) for v in range(1, NN + 1): for u in adj_new[v]: in_num[u] += 1 out_num[v] += 1 if NN == 1: print(0) exit() seen = [0] * (NN+1) v0_list = [] plus_sum = minus_sum = 0 for v0 in range(1, NN+1): if seen[v0]: continue v0_list.append(v0) seen[v0] = 1 que = deque() que.append(v0) seq = [] while que: v = que.popleft() seq.append(v) for u in adj_new[v]: if seen[u] == 0: seen[u] = 1 que.append(u) p = s = 0 for v in seq: if in_num[v] == 0: p += 1 if out_num[v] == 0: s += 1 plus_sum += p minus_sum += s print(max(plus_sum, minus_sum)) if __name__ == '__main__': main()