結果

問題 No.1813 Magical Stones
ユーザー mkawa2mkawa2
提出日時 2024-01-23 16:36:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 480 ms / 2,000 ms
コード長 2,242 bytes
コンパイル時間 366 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 117,516 KB
最終ジャッジ日時 2024-01-23 16:37:03
合計ジャッジ時間 12,586 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,460 KB
testcase_01 AC 36 ms
53,460 KB
testcase_02 AC 38 ms
53,460 KB
testcase_03 AC 90 ms
93,896 KB
testcase_04 AC 37 ms
53,460 KB
testcase_05 AC 37 ms
53,460 KB
testcase_06 AC 92 ms
76,844 KB
testcase_07 AC 38 ms
53,460 KB
testcase_08 AC 52 ms
64,196 KB
testcase_09 AC 55 ms
66,272 KB
testcase_10 AC 46 ms
61,700 KB
testcase_11 AC 143 ms
85,668 KB
testcase_12 AC 38 ms
53,460 KB
testcase_13 AC 37 ms
53,460 KB
testcase_14 AC 87 ms
93,892 KB
testcase_15 AC 480 ms
108,812 KB
testcase_16 AC 444 ms
110,988 KB
testcase_17 AC 412 ms
117,516 KB
testcase_18 AC 475 ms
110,476 KB
testcase_19 AC 457 ms
110,220 KB
testcase_20 AC 474 ms
110,092 KB
testcase_21 AC 450 ms
110,604 KB
testcase_22 AC 458 ms
110,996 KB
testcase_23 AC 462 ms
110,988 KB
testcase_24 AC 75 ms
74,820 KB
testcase_25 AC 117 ms
79,880 KB
testcase_26 AC 102 ms
77,704 KB
testcase_27 AC 315 ms
97,668 KB
testcase_28 AC 379 ms
99,860 KB
testcase_29 AC 362 ms
105,268 KB
testcase_30 AC 362 ms
99,488 KB
testcase_31 AC 450 ms
109,508 KB
testcase_32 AC 37 ms
53,460 KB
testcase_33 AC 37 ms
53,460 KB
testcase_34 AC 101 ms
77,328 KB
testcase_35 AC 99 ms
77,460 KB
testcase_36 AC 89 ms
76,860 KB
testcase_37 AC 137 ms
81,668 KB
testcase_38 AC 125 ms
90,692 KB
testcase_39 AC 327 ms
102,012 KB
testcase_40 AC 96 ms
76,944 KB
testcase_41 AC 74 ms
76,264 KB
testcase_42 AC 97 ms
77,448 KB
testcase_43 AC 114 ms
77,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(1000005)
# sys.set_int_max_str_digits(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
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 = -1-(-1 << 31)
inf = -1-(-1 << 62)

# md = 10**9+7
md = 998244353

# 各強連結成分をリストにしてトポロジカル順に返す
def SCC(to, ot):
    n = len(to)
    # トポロジカルソート
    fin = [-1]*n
    topo = []
    for u in range(n):
        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

n,m=LI()
to=[[] for _ in range(n)]
ot=[[] for _ in range(n)]
for _ in range(m):
    u,v=LI1()
    to[u].append(v)
    ot[v].append(u)

gg=SCC(to,ot)
gn=len(gg)

if gn==1:
    print(0)
    exit()

u2g=[0]*n
for g,uu in enumerate(gg):
    for u in uu:
        u2g[u]=g

indeg=[0]*gn
outdeg=[0]*gn
for u in range(n):
    g=u2g[u]
    for v in to[u]:
        h=u2g[v]
        if g==h:continue
        outdeg[g]+=1
        indeg[h]+=1

print(max(indeg.count(0),outdeg.count(0)))
0