結果

問題 No.1813 Magical Stones
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2022-01-14 22:28:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 3,217 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 81,992 KB
実行使用メモリ 137,264 KB
最終ジャッジ日時 2024-11-20 12:02:01
合計ジャッジ時間 7,433 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
52,096 KB
testcase_01 AC 36 ms
52,480 KB
testcase_02 AC 36 ms
52,096 KB
testcase_03 AC 91 ms
99,428 KB
testcase_04 AC 36 ms
52,480 KB
testcase_05 AC 36 ms
52,480 KB
testcase_06 AC 80 ms
77,184 KB
testcase_07 AC 39 ms
52,864 KB
testcase_08 AC 55 ms
65,536 KB
testcase_09 AC 60 ms
68,352 KB
testcase_10 AC 46 ms
62,592 KB
testcase_11 AC 115 ms
83,720 KB
testcase_12 AC 37 ms
53,120 KB
testcase_13 AC 35 ms
52,736 KB
testcase_14 AC 88 ms
99,384 KB
testcase_15 AC 258 ms
120,752 KB
testcase_16 AC 266 ms
120,888 KB
testcase_17 AC 219 ms
137,264 KB
testcase_18 AC 271 ms
121,040 KB
testcase_19 AC 250 ms
120,460 KB
testcase_20 AC 268 ms
120,772 KB
testcase_21 AC 266 ms
120,248 KB
testcase_22 AC 254 ms
120,500 KB
testcase_23 AC 265 ms
121,132 KB
testcase_24 AC 70 ms
73,536 KB
testcase_25 AC 103 ms
81,100 KB
testcase_26 AC 87 ms
77,580 KB
testcase_27 AC 217 ms
111,752 KB
testcase_28 AC 236 ms
114,488 KB
testcase_29 AC 239 ms
111,940 KB
testcase_30 AC 229 ms
109,352 KB
testcase_31 AC 260 ms
123,820 KB
testcase_32 AC 36 ms
53,816 KB
testcase_33 AC 36 ms
53,488 KB
testcase_34 AC 87 ms
77,208 KB
testcase_35 AC 87 ms
82,508 KB
testcase_36 AC 82 ms
77,404 KB
testcase_37 AC 122 ms
87,040 KB
testcase_38 AC 108 ms
90,540 KB
testcase_39 AC 206 ms
103,220 KB
testcase_40 AC 88 ms
76,924 KB
testcase_41 AC 67 ms
77,152 KB
testcase_42 AC 84 ms
82,828 KB
testcase_43 AC 104 ms
78,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""

グラフを強連結にすればよい
とりあえず SCCする
DAGになる

S1,G1
S2,G2
...
S3,G3

さて, StartとGoalがあるやつは
上手くやればくっ付けられる

"""

import sys
from sys import stdin
import sys


class scc_graph:
 
    def __init__(self, N):
        self.N = N
        self.edges = []
 
    def csr(self):
        self.start = [0]*(self.N+1)
        self.elist = [0]*len(self.edges)
        for e in self.edges:
            self.start[e[0]+1] += 1
        for i in range(1, self.N+1):
            self.start[i] += self.start[i-1]
        counter = self.start[:]
        for e in self.edges:
            self.elist[counter[e[0]]] = e[1]
            counter[e[0]] += 1
 
    def add_edge(self, v, w):
        self.edges.append((v, w))
 
    def scc_ids(self):
        self.csr()
        N = self.N
        now_ord = group_num = 0
        visited = []
        low = [0]*N
        order = [-1]*N
        ids = [0]*N
        parent = [-1]*N
        stack = []
        for i in range(N):
            if order[i] == -1:
                stack.append(i)
                stack.append(i)
                while stack:
                    v = stack.pop()
                    if order[v] == -1:
                        low[v] = order[v] = now_ord
                        now_ord += 1
                        visited.append(v)
                        for i in range(self.start[v], self.start[v+1]):
                            to = self.elist[i]
                            if order[to] == -1:
                                stack.append(to)
                                stack.append(to)
                                parent[to] = v
                            else:
                                low[v] = min(low[v], order[to])
                    else:
                        if low[v] == order[v]:
                            while True:
                                u = visited.pop()
                                order[u] = N
                                ids[u] = group_num
                                if u == v:
                                    break
                            group_num += 1
                        if parent[v] != -1:
                            low[parent[v]] = min(low[parent[v]], low[v])
        for i, x in enumerate(ids):
            ids[i] = group_num-1-x
 
        return group_num, ids
 
    def scc(self):
        group_num, ids = self.scc_ids()
        groups = [[] for _ in range(group_num)]
        for i, x in enumerate(ids):
            groups[x].append(i)
        return groups


N,M = map(int,stdin.readline().split())

AB = []
g = scc_graph(N)

for i in range(M):

    A,B = map(int,stdin.readline().split())
    A -= 1;B -= 1
    AB.append( (A,B) )
    g.add_edge(A,B)

scc = g.scc()

dic = {}
for i in range(len(scc)):
    for v in scc[i]:
        dic[v] = i

inlis = [0] * len(scc)
olis = [0] * len(scc)
if len(scc) == 1:
    print (0)
    sys.exit()

for A,B in AB:
    if dic[A] == dic[B]:
        continue
    olis[ dic[A] ] += 1
    inlis[dic[B]] += 1

inone = 0
onone = 0

for i in inlis:
    if i == 0:
        inone += 1
for i in olis:
    if i == 0:
        onone += 1

ans = max(inone,onone)
print (ans)     
0