結果

問題 No.1813 Magical Stones
ユーザー ああいいああいい
提出日時 2022-03-04 23:59:23
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,758 bytes
コンパイル時間 304 ms
コンパイル使用メモリ 86,960 KB
実行使用メモリ 118,256 KB
最終ジャッジ日時 2023-09-26 03:37:53
合計ジャッジ時間 16,623 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,324 KB
testcase_01 AC 70 ms
71,004 KB
testcase_02 AC 70 ms
71,000 KB
testcase_03 AC 133 ms
102,840 KB
testcase_04 AC 70 ms
71,324 KB
testcase_05 AC 69 ms
71,308 KB
testcase_06 AC 164 ms
80,072 KB
testcase_07 AC 71 ms
71,284 KB
testcase_08 AC 91 ms
76,788 KB
testcase_09 AC 99 ms
76,636 KB
testcase_10 AC 82 ms
76,068 KB
testcase_11 AC 237 ms
93,300 KB
testcase_12 AC 71 ms
71,336 KB
testcase_13 AC 71 ms
71,420 KB
testcase_14 AC 130 ms
103,048 KB
testcase_15 AC 542 ms
118,104 KB
testcase_16 WA -
testcase_17 AC 506 ms
116,140 KB
testcase_18 AC 575 ms
117,200 KB
testcase_19 AC 561 ms
118,256 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 119 ms
77,968 KB
testcase_25 AC 201 ms
87,164 KB
testcase_26 AC 177 ms
80,648 KB
testcase_27 AC 432 ms
108,332 KB
testcase_28 AC 439 ms
100,236 KB
testcase_29 AC 487 ms
108,976 KB
testcase_30 AC 435 ms
103,700 KB
testcase_31 WA -
testcase_32 AC 69 ms
71,432 KB
testcase_33 AC 71 ms
71,020 KB
testcase_34 WA -
testcase_35 AC 139 ms
78,948 KB
testcase_36 AC 149 ms
80,292 KB
testcase_37 AC 177 ms
80,316 KB
testcase_38 AC 212 ms
103,684 KB
testcase_39 WA -
testcase_40 AC 170 ms
80,024 KB
testcase_41 AC 118 ms
77,692 KB
testcase_42 AC 139 ms
78,460 KB
testcase_43 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#強連結成分分解
#分解したら、sG を 頂点数num のグラフとして普通に扱えばいい
#sG は set で作ってある
#Componentに各成分を構成するもとの頂点を入れてる
class SCC():

    #index :0-index or 1-index
    #逆グラフは自動で作る
    def __init__(self,G,index = 1):
        self.G = G
        self.N = len(G)
        self.index = index
        self.rG = self.make_reverse()
        self.order = []
        self.parent = [0] * self.N  #各頂点の属する強連結成分の番号

        self.num = 0     #強連結成分の数
        
        self.build()
        self.sG,self.top,self.Component = self.make_sG()
        
        #sG 縮約後のグラフ。setでつくってある
        #top sGをトポロジカルソートしたもの
        #Component 各強連結成分に属する頂点をまとめた配列

    #逆グラフを作る
    def make_reverse(self):
        G = self.G
        rG = [[] for _ in range(self.N)]
        for v in range(self.index,self.N):
            for u in G[v]:
                rG[u].append(v)
        return rG
    def build(self):
        G = self.G
        rG = self.rG
        parent = self.parent
        order = self.order

        memo = [0] * self.N
        stack = []
        label = 0
        for v in range(self.index,self.N):
            if memo[v] == 1:continue
            stack.append(~v)
            stack.append(v)
            memo[v] = 1
            while stack:
                now = stack.pop()
                if now >= 0:
                    for u in G[now]:
                        if memo[u] == 0:
                            memo[u] = 1
                            stack.append(~u)
                            stack.append(u)
                else:
                    order.append(~now)
        memo = [0] * self.N
        for v in reversed(order):
            if memo[v] == 1:continue
            stack.append(v)
            memo[v] = 1
            parent[v] = label
            while stack:
                now = stack.pop()
                for u in rG[now]:
                    if memo[u] == 1:
                        continue
                    memo[u] = 1
                    stack.append(u)
                    parent[u] = label
            label += 1
        self.num = label
    #縮約後のグラフを作る
    #トポロジカルソートもする
    def make_sG(self):
        sG = [set() for _ in range(self.num)]
        sGnum = [0] * self.num
        Component = [[] for _ in range(self.num)]
        G = self.G
        parent = self.parent
        for v in range(self.index,self.N):
            Component[parent[v]].append(v)
            for u in G[v]:
                if parent[u] == parent[v]:continue
                if parent[u] not in sG[parent[v]]:
                    sG[parent[v]].add(parent[u])
                    sGnum[parent[u]] += 1
        top = []
        stack = []
        for i in range(self.num):
            if sGnum[i] == 0:
                stack.append(i)
        while stack:
            now = stack.pop()
            top.append(now)
            for v in sG[now]:
                sGnum[v] -= 1
                if sGnum[v] == 0:
                    stack.append(v)
        return sG,top,Component
        
N,M = map(int,input().split())
G = [[] for _ in range(N + 1)]
for _ in range(M):
    a,b = map(int,input().split())
    G[a].append(b)
scc = SCC(G,1)
sG = scc.sG
n = scc.num
inGnum = [0] * n
outGnum = [0] * n
for i in range(n):
    for v in sG[i]:
        inGnum[v] += 1
        outGnum[i] += 1
import sys
if n == 1:
    print(0)
    exit()
_in = 0
out = 0
for i in range(n):
    if inGnum[i] == 0:
        _in += 1
    if outGnum[i] == 0:
        out += 1
print(max(_in,out))
0