結果

問題 No.1813 Magical Stones
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2022-01-20 23:10:37
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,579 bytes
コンパイル時間 2,645 ms
コンパイル使用メモリ 86,892 KB
実行使用メモリ 123,956 KB
最終ジャッジ日時 2023-08-15 23:47:27
合計ジャッジ時間 16,784 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,368 KB
testcase_01 AC 68 ms
71,120 KB
testcase_02 AC 69 ms
71,264 KB
testcase_03 AC 111 ms
86,124 KB
testcase_04 AC 70 ms
71,524 KB
testcase_05 WA -
testcase_06 AC 135 ms
79,016 KB
testcase_07 AC 75 ms
71,384 KB
testcase_08 AC 88 ms
76,680 KB
testcase_09 AC 97 ms
77,284 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 73 ms
71,320 KB
testcase_13 AC 69 ms
71,440 KB
testcase_14 AC 105 ms
86,432 KB
testcase_15 WA -
testcase_16 AC 496 ms
108,640 KB
testcase_17 AC 443 ms
123,956 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 437 ms
108,852 KB
testcase_21 AC 457 ms
109,276 KB
testcase_22 AC 444 ms
109,328 KB
testcase_23 AC 418 ms
108,536 KB
testcase_24 WA -
testcase_25 AC 204 ms
81,904 KB
testcase_26 AC 159 ms
79,456 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 442 ms
113,036 KB
testcase_32 AC 69 ms
71,264 KB
testcase_33 AC 71 ms
71,140 KB
testcase_34 AC 163 ms
79,908 KB
testcase_35 AC 152 ms
82,404 KB
testcase_36 AC 133 ms
78,764 KB
testcase_37 AC 213 ms
87,796 KB
testcase_38 AC 150 ms
83,880 KB
testcase_39 AC 358 ms
99,156 KB
testcase_40 WA -
testcase_41 AC 108 ms
77,880 KB
testcase_42 AC 148 ms
82,588 KB
testcase_43 AC 192 ms
80,684 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)
uf = Unionfind(n)
for i in range(m):
    a,b = [int(x)-1 for x in input().split()]
    scc.add_edge(a,b)
    uf.union(a,b)


group = scc.scc()

groups = [0]*n

used = [0]*n
for i,g in enumerate(group):

    for j in g:
        groups[j] = i
        if used[uf.find(j)]:
            continue
        used[uf.find(j)] = 1
ans = 0

    
used2 = [0]*len(group)
for a,b in scc.edges:
    if groups[a] != groups[b]:
        used2[groups[a]] = 1
for u in used2:
    if u:
        continue
    ans += 1

if len(used2) == 1:
    ans = 0
print(ans)
0