結果

問題 No.1813 Magical Stones
ユーザー ygd.ygd.
提出日時 2022-01-15 00:02:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 814 ms / 2,000 ms
コード長 4,242 bytes
コンパイル時間 264 ms
コンパイル使用メモリ 82,848 KB
実行使用メモリ 123,208 KB
最終ジャッジ日時 2024-04-30 18:46:11
合計ジャッジ時間 18,316 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
84,556 KB
testcase_01 AC 97 ms
84,620 KB
testcase_02 AC 96 ms
84,356 KB
testcase_03 AC 170 ms
109,396 KB
testcase_04 AC 101 ms
84,100 KB
testcase_05 AC 99 ms
84,748 KB
testcase_06 AC 164 ms
88,188 KB
testcase_07 AC 105 ms
84,504 KB
testcase_08 AC 113 ms
87,288 KB
testcase_09 AC 124 ms
87,776 KB
testcase_10 AC 107 ms
84,608 KB
testcase_11 AC 369 ms
97,104 KB
testcase_12 AC 103 ms
84,496 KB
testcase_13 AC 102 ms
84,348 KB
testcase_14 AC 180 ms
109,516 KB
testcase_15 AC 778 ms
120,644 KB
testcase_16 AC 749 ms
121,672 KB
testcase_17 AC 723 ms
123,208 KB
testcase_18 AC 785 ms
120,540 KB
testcase_19 AC 787 ms
120,124 KB
testcase_20 AC 814 ms
121,404 KB
testcase_21 AC 803 ms
121,680 KB
testcase_22 AC 802 ms
121,768 KB
testcase_23 AC 779 ms
121,152 KB
testcase_24 AC 147 ms
87,896 KB
testcase_25 AC 313 ms
92,260 KB
testcase_26 AC 217 ms
89,260 KB
testcase_27 AC 657 ms
112,840 KB
testcase_28 AC 585 ms
111,640 KB
testcase_29 AC 658 ms
114,844 KB
testcase_30 AC 630 ms
110,916 KB
testcase_31 AC 799 ms
120,120 KB
testcase_32 AC 102 ms
84,096 KB
testcase_33 AC 100 ms
84,156 KB
testcase_34 AC 212 ms
88,172 KB
testcase_35 AC 180 ms
87,900 KB
testcase_36 AC 165 ms
88,192 KB
testcase_37 AC 287 ms
91,116 KB
testcase_38 AC 232 ms
102,608 KB
testcase_39 AC 619 ms
113,704 KB
testcase_40 AC 184 ms
88,596 KB
testcase_41 AC 136 ms
87,692 KB
testcase_42 AC 175 ms
87,972 KB
testcase_43 AC 247 ms
90,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from tkinter import N
#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))
    # label。不要なら消しても良い。
    label = [-1] * n
    for i in range(len(sccs)):
        for x in sccs[i]:
            label[x] = i
    return sccs, label #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)
    
    L, label = strongly_connected_components(N,G,RG)
    if len(L) == 1:
        print(0);exit()

    #print(label)
    IN = [0]*len(L)
    OUT = [0]*len(L)
    for v in range(N):
        for u in G[v]:
            if label[v] == label[u]: continue
            IN[label[v]] += 1
        for u in RG[v]:
            if label[v] == label[u]: continue
            OUT[label[v]] += 1
    #print(IN,OUT)
    ans = max(IN.count(0), OUT.count(0))
    print(ans)
    

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