結果
問題 | No.1194 Replace |
ユーザー | mkawa2 |
提出日時 | 2021-09-20 22:11:42 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,139 ms / 2,000 ms |
コード長 | 2,288 bytes |
コンパイル時間 | 211 ms |
コンパイル使用メモリ | 82,456 KB |
実行使用メモリ | 319,408 KB |
最終ジャッジ日時 | 2024-07-02 23:00:35 |
合計ジャッジ時間 | 21,329 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 812 ms
251,264 KB |
testcase_01 | AC | 885 ms
253,496 KB |
testcase_02 | AC | 730 ms
224,968 KB |
testcase_03 | AC | 608 ms
191,508 KB |
testcase_04 | AC | 877 ms
257,456 KB |
testcase_05 | AC | 834 ms
250,780 KB |
testcase_06 | AC | 729 ms
231,004 KB |
testcase_07 | AC | 1,097 ms
319,216 KB |
testcase_08 | AC | 1,122 ms
316,532 KB |
testcase_09 | AC | 1,129 ms
316,380 KB |
testcase_10 | AC | 1,139 ms
316,472 KB |
testcase_11 | AC | 1,119 ms
316,560 KB |
testcase_12 | AC | 1,132 ms
319,408 KB |
testcase_13 | AC | 646 ms
168,328 KB |
testcase_14 | AC | 569 ms
148,824 KB |
testcase_15 | AC | 528 ms
162,184 KB |
testcase_16 | AC | 616 ms
162,768 KB |
testcase_17 | AC | 489 ms
147,756 KB |
testcase_18 | AC | 451 ms
138,984 KB |
testcase_19 | AC | 637 ms
171,112 KB |
testcase_20 | AC | 38 ms
54,272 KB |
testcase_21 | AC | 38 ms
53,888 KB |
testcase_22 | AC | 37 ms
54,144 KB |
testcase_23 | AC | 280 ms
117,676 KB |
testcase_24 | AC | 171 ms
94,848 KB |
testcase_25 | AC | 108 ms
78,636 KB |
testcase_26 | AC | 253 ms
99,584 KB |
testcase_27 | AC | 110 ms
78,360 KB |
testcase_28 | AC | 172 ms
87,464 KB |
testcase_29 | AC | 91 ms
77,816 KB |
ソースコード
import sys # sys.setrecursionlimit(200005) int1 = lambda x: int(x)-1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = 10**16 # md = 998244353 md = 10**9+7 # 各強連結成分をリストにしてトポロジカル順に返す def SCC(to, ot, ver): # トポロジカルソート fin = defaultdict(lambda: -1) topo = [] for u in ver: if fin[u] != -1: continue stack = [u] while stack: u = stack[-1] if fin[u] == -1: fin[u] = 0 for v in to[u]: if fin[v] != -1: continue stack.append(v) else: stack.pop() if fin[u] == 0: fin[u] = 1 topo.append(u) # 逆辺でdfs res = [] while topo: u = topo.pop() if fin[u] != 1: continue fin[u] = 2 cur = [u] i = 0 while i < len(cur): u = cur[i] for v in ot[u]: if fin[v] == 2: continue fin[v] = 2 cur.append(v) i += 1 res.append(cur) return res from collections import defaultdict n, m = LI() to = defaultdict(list) ot = defaultdict(list) ver = set() for _ in range(m): b, c = LI() to[b].append(c) ot[c].append(b) ver.add(b) ver.add(c) # print(to) # print(ot) gg = SCC(to, ot, ver) gn = len(gg) # print(gg) val = [] utog = defaultdict(int) for g, uu in enumerate(gg): val.append(max(uu)) for u in uu: utog[u] = g for g in range(gn)[::-1]: uu = gg[g] for u in uu: for v in ot[u]: h = utog[v] if g == h: continue val[h] = max(val[h], val[g]) ans = n*(n+1)//2 for g, uu in enumerate(gg): ans += val[g]*len(uu)-sum(uu) print(ans)