結果

問題 No.1813 Magical Stones
ユーザー tamatotamato
提出日時 2022-01-14 22:54:56
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,301 bytes
コンパイル時間 263 ms
コンパイル使用メモリ 82,512 KB
実行使用メモリ 112,064 KB
最終ジャッジ日時 2024-11-20 13:28:59
合計ジャッジ時間 12,610 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
54,952 KB
testcase_01 AC 42 ms
54,848 KB
testcase_02 AC 42 ms
54,964 KB
testcase_03 AC 108 ms
97,264 KB
testcase_04 AC 43 ms
54,400 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 45 ms
55,860 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 109 ms
97,436 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 451 ms
105,144 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 46 ms
54,528 KB
testcase_33 AC 44 ms
54,528 KB
testcase_34 WA -
testcase_35 AC 103 ms
77,440 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 76 ms
74,880 KB
testcase_42 AC 101 ms
77,440 KB
testcase_43 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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
        if p == 0:
            p = 1
        if s == 0:
            s = 1
        plus_sum += p
        minus_sum += s
    print(max(plus_sum, minus_sum))


if __name__ == '__main__':
    main()
0