結果

問題 No.1813 Magical Stones
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2022-01-20 23:16:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 230 ms / 2,000 ms
コード長 3,477 bytes
コンパイル時間 206 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 116,396 KB
最終ジャッジ日時 2024-11-24 09:16:37
合計ジャッジ時間 7,050 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
52,608 KB
testcase_01 AC 34 ms
52,224 KB
testcase_02 AC 38 ms
52,608 KB
testcase_03 AC 79 ms
85,760 KB
testcase_04 AC 39 ms
52,608 KB
testcase_05 AC 34 ms
53,120 KB
testcase_06 AC 78 ms
76,484 KB
testcase_07 AC 36 ms
53,504 KB
testcase_08 AC 49 ms
64,512 KB
testcase_09 AC 54 ms
67,200 KB
testcase_10 AC 45 ms
62,336 KB
testcase_11 AC 114 ms
82,588 KB
testcase_12 AC 34 ms
52,608 KB
testcase_13 AC 34 ms
52,608 KB
testcase_14 AC 75 ms
86,016 KB
testcase_15 AC 221 ms
104,624 KB
testcase_16 AC 225 ms
104,752 KB
testcase_17 AC 201 ms
116,396 KB
testcase_18 AC 219 ms
103,856 KB
testcase_19 AC 218 ms
105,384 KB
testcase_20 AC 230 ms
104,368 KB
testcase_21 AC 224 ms
103,472 KB
testcase_22 AC 216 ms
105,516 KB
testcase_23 AC 230 ms
104,364 KB
testcase_24 AC 67 ms
75,392 KB
testcase_25 AC 97 ms
78,464 KB
testcase_26 AC 82 ms
76,800 KB
testcase_27 AC 177 ms
91,996 KB
testcase_28 AC 200 ms
104,752 KB
testcase_29 AC 205 ms
96,820 KB
testcase_30 AC 201 ms
101,412 KB
testcase_31 AC 212 ms
103,984 KB
testcase_32 AC 35 ms
52,608 KB
testcase_33 AC 36 ms
52,608 KB
testcase_34 AC 88 ms
77,852 KB
testcase_35 AC 89 ms
80,840 KB
testcase_36 AC 78 ms
76,800 KB
testcase_37 AC 119 ms
85,224 KB
testcase_38 AC 103 ms
88,176 KB
testcase_39 AC 185 ms
94,100 KB
testcase_40 AC 84 ms
76,676 KB
testcase_41 AC 69 ms
76,672 KB
testcase_42 AC 87 ms
81,080 KB
testcase_43 AC 102 ms
77,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class SCC:

    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,u,v):
        self.edges.append((u,v))

    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 i in range(group_num)]
        for i,x in enumerate(ids):
            groups[x].append(i)

        return groups


    
class Unionfind:
     
    def __init__(self,n):
        self.uf = [-1]*n
 
    def find(self,x):
        if self.uf[x] < 0:
            return x
        else:
            self.uf[x] = self.find(self.uf[x])
            return self.uf[x]
 
    def same(self,x,y):
        return self.find(x) == self.find(y)
 
    def union(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.uf[x] > self.uf[y]:
            x,y = y,x
        self.uf[x] += self.uf[y]
        self.uf[y] = x
        return True
 
    def size(self,x):
        x = self.find(x)
        return -self.uf[x]



n,m = map(int,input().split())
scc = SCC(n)

for i in range(m):
    a,b = [int(x)-1 for x in input().split()]
    scc.add_edge(a,b)



group = scc.scc()

groups = [0]*n

for i,g in enumerate(group):

    for j in g:
        groups[j] = i
ans = 0

    
used = [1]*len(group)
used2 = [1]*len(group)
for a,b in scc.edges:
    if groups[a] != groups[b]:
        used[groups[a]] = 0
        used2[groups[b]] = 0
ans = max(sum(used),sum(used2))
if len(used) == 1:
    ans = 0
print(ans)
0