結果

問題 No.1553 Lovely City
ユーザー ygd.ygd.
提出日時 2021-06-18 22:34:01
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 3,209 bytes
コンパイル時間 615 ms
コンパイル使用メモリ 87,168 KB
実行使用メモリ 655,032 KB
最終ジャッジ日時 2023-09-04 23:45:29
合計ジャッジ時間 9,589 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 AC 94 ms
71,944 KB
testcase_02 AC 93 ms
71,688 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 113 ms
76,920 KB
testcase_06 AC 114 ms
76,768 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 MLE -
testcase_10 AC 1,245 ms
170,608 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 TLE -
testcase_17 AC 1,040 ms
149,916 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 -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**7)
from collections import defaultdict 

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 main():
    N,M = map(int,input().split())
    Q = []
    S = set([])
    for i in range(M):
        U,V = map(int,input().split())
        Q.append((U,V))
        S.add(U)
        S.add(V)
    L = list(S)
    n = len(S) #出てくる頂点の種類
    L.sort()
    dic = {} #元の番号→座圧後の番号
    revdic = {} #座圧後の番号→元の番号
    for i in range(n):
        dic[L[i]] = i
        revdic[i] = L[i]
    ALL = [i for i in range(n)]
    ALL = set(ALL)
    G = [[] for _ in range(n)]
    uf = UnionFind(n)
    for u,v in Q:
        uz = dic[u]
        vz = dic[v]
        G[uz].append(vz)
        uf.union(uz,vz)
    #print(G)
    unidic = defaultdict(list)
    for i in range(n):
        par = uf.find(i)
        unidic[par].append(i)
    
    #print(unidic)

    #連結成分ごと
    ans = []
    for L in unidic.values():
        ALL = set(L)
        for v in L:
            for u in G[v]:
                ALL.discard(u)
        
        if not ALL:
            for i in range(len(L)):
                if i+1 < len(L):
                    s = L[i]; t = L[i+1]
                else:
                    s = L[i]; t = L[0]
                sori = revdic[s]; tori = revdic[t]
                ans.append([sori,tori])
            continue
        #print(ALL)
        used = set([])

        def dfs(v):
            if v in used:
                return
            used.add(v)
            for w in G[v]:
                if w in used:
                    continue
                dfs(w)
                TS.append(w) #帰りがけで記録

        TS = []
        for root in ALL: #すべての始点について
            dfs(root)
            TS.append(root)
        TS.reverse() #最後に反転

        for i in range(len(TS)-1):
            s = TS[i]; t = TS[i+1]
            sori = revdic[s]; tori = revdic[t]
            ret = [sori, tori]
            ans.append(ret)
        
    print(len(ans))
    for ret in ans:
        print(*ret)


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