結果

問題 No.1813 Magical Stones
ユーザー ygd.ygd.
提出日時 2022-01-14 23:15:39
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,882 bytes
コンパイル時間 404 ms
コンパイル使用メモリ 82,436 KB
実行使用メモリ 119,144 KB
最終ジャッジ日時 2024-04-30 17:12:08
合計ジャッジ時間 14,287 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
53,992 KB
testcase_01 AC 49 ms
53,744 KB
testcase_02 AC 52 ms
53,764 KB
testcase_03 AC 73 ms
85,240 KB
testcase_04 AC 45 ms
54,052 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 68 ms
85,716 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 847 ms
119,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 45 ms
53,604 KB
testcase_33 AC 42 ms
54,332 KB
testcase_34 WA -
testcase_35 AC 125 ms
77,344 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 83 ms
76,296 KB
testcase_42 AC 127 ms
77,724 KB
testcase_43 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
#input = sys.stdin.readline #文字列につけてはダメ
input = sys.stdin.buffer.readline #文字列につけてはダメ
#sys.setrecursionlimit(1000000)
#import bisect
#import itertools
#import random
from heapq import heapify, heappop, heappush
#from collections import defaultdict 
#from collections import deque
#import copy
#import math
#from functools import lru_cache
#MOD = pow(10,9) + 7
#MOD = 998244353

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]
    def find(self, x):
        """
        x が属するグループを探索して親を出す。
        """
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
    def union(self, x, y):
        """
        x と y のグループを結合
        """
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]
    def is_same(self, x, y):
        """
        x と y が同じグループか否か
        """
        return self.find(x) == self.find(y)
    def get_size(self, x):
        """
        x が属するグループの要素数
        """
        x = self.find(x)
        return self.size[x]

def topological(graph, deg):
    start = []
    for i in range(len(deg)):
        if deg[i] == 0:
            start.append(i)
    topo = []
    while start:
        v = start.pop()
        topo.append(v)
        for u in graph[v]:
            deg[u] -= 1
            if deg[u] == 0:
                start.append(u)
    return topo

#https://atcoder.jp/contests/arc030/submissions/20496099より拝借
def scc_dfs1(s, links, status, postorder):
    stack = [s]
    status[s] = 0
    while stack:
        v = stack[-1]
        limit = len(links[v])
        while status[v] < limit:
            u = links[v][status[v]]
            status[v] += 1
            if status[u] != -1:
                continue
            stack.append(u)
            status[u] = 0
            break
        else:
            stack.pop()
            postorder.append(v)
    return
def scc_dfs2(s, rev_links, status):
    stack = [s]
    status[s] = -1
    scc = [s]
    while stack:
        v = stack.pop()
        for u in rev_links[v]:
            if status[u] != -1:
                stack.append(u)
                status[u] = -1
                scc.append(u)
    scc.reverse() #ループの順番が正の順番になるようにする。#要注意
    return scc
def strongly_connected_components(n, links, rev_links):
    status = [-1] * n
    postorder = []
    for v in range(n):
        if status[v] != -1:
            continue
        scc_dfs1(v, links, status, postorder)
    postorder.reverse()
    sccs = []
    for v in postorder:
        if status[v] == -1:
            continue
        sccs.append(scc_dfs2(v, rev_links, status))
    return sccs #DAG順になったグループごとのリスト

def main():
    N,M = map(int,input().split())
    G = [[] for _ in range(N)]
    RG = [[] for _ in range(N)]
    uf = UnionFind(N)
    for i in range(M):
        a,b = map(int,input().split())
        a -= 1; b -= 1
        G[a].append(b)
        RG[b].append(a)
        uf.union(a,b)
    
    if uf.get_size(0) == N: #全てが連結
        L = strongly_connected_components(N,G,RG)
        #print(L)
        if len(L) == 1:
            print(0)
        else:
            print(1)
        exit()
    
    group = set([])
    for i in range(N):
        group.add(uf.find(i))
    ans = len(group)
    print(ans)
    

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