結果

問題 No.1553 Lovely City
ユーザー convexineqconvexineq
提出日時 2021-06-18 22:49:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 630 ms / 2,000 ms
コード長 1,921 bytes
コンパイル時間 345 ms
コンパイル使用メモリ 86,932 KB
実行使用メモリ 138,168 KB
最終ジャッジ日時 2023-09-04 23:56:30
合計ジャッジ時間 17,022 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
71,572 KB
testcase_01 AC 91 ms
71,648 KB
testcase_02 AC 245 ms
130,148 KB
testcase_03 AC 98 ms
71,972 KB
testcase_04 AC 102 ms
76,216 KB
testcase_05 AC 105 ms
76,820 KB
testcase_06 AC 101 ms
76,660 KB
testcase_07 AC 101 ms
76,220 KB
testcase_08 AC 486 ms
110,312 KB
testcase_09 AC 470 ms
111,884 KB
testcase_10 AC 568 ms
126,108 KB
testcase_11 AC 434 ms
104,760 KB
testcase_12 AC 590 ms
128,476 KB
testcase_13 AC 415 ms
103,200 KB
testcase_14 AC 461 ms
108,300 KB
testcase_15 AC 459 ms
111,668 KB
testcase_16 AC 481 ms
115,188 KB
testcase_17 AC 473 ms
113,472 KB
testcase_18 AC 629 ms
132,428 KB
testcase_19 AC 606 ms
135,448 KB
testcase_20 AC 628 ms
133,940 KB
testcase_21 AC 617 ms
133,132 KB
testcase_22 AC 630 ms
138,168 KB
testcase_23 AC 612 ms
133,272 KB
testcase_24 AC 612 ms
133,524 KB
testcase_25 AC 603 ms
134,136 KB
testcase_26 AC 624 ms
129,452 KB
testcase_27 AC 620 ms
133,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n)) #親ノード
        self.size = [1]*n #グループの要素数
 
    def root(self, x): #root(x): xの根ノードを返す.
        while self.parent[x] != x:
            self.parent[x] = self.parent[self.parent[x]]
            x = self.parent[x]
        return x 
 
    def merge(self, x, y): #merge(x,y): xのいる組とyのいる組をまとめる
        x, y = self.root(x), self.root(y)
        if x == y: return False
        if self.size[x] < self.size[y]: x,y=y,x #xの要素数が大きいように
        self.size[x] += self.size[y] #xの要素数を更新
        self.parent[y] = x #yをxにつなぐ
        return True
 
    def issame(self, x, y): #same(x,y): xとyが同じ組ならTrue
        return self.root(x) == self.root(y)
        
    def getsize(self,x): #size(x): xのいるグループの要素数を返す
        return self.size[self.root(x)]

import sys
readline = sys.stdin.readline

n,m = map(int,readline().split())
g = [[] for _ in range(n)]

UF = UnionFind(n+1)
g = [[] for _ in range(n+1)]
ind = [0]*(n+1)

for _ in range(m):
    a,b = map(int,readline().split())
    g[a].append(b)
    ind[b] += 1
    UF.merge(a,b)

from collections import defaultdict
d = defaultdict(list)
for i in range(1,n+1):
    r = UF.root(i)
    d[r].append(i)

def top_sort(lst):
    q = [i for i in lst if ind[i]==0]
    res = []
    while q:
        v = q.pop()
        res.append(v)
        for c in g[v]:
            ind[c] -= 1
            if ind[c] == 0:
                q.append(c)
    return res

ans = []
for r,lst in d.items():
    ts = top_sort(lst)
    if len(ts) == len(lst):
        for i,j in zip(ts,ts[1:]):
            ans.append((i,j))
    else:
        for i,j in zip(lst,lst[1:]):
            ans.append((i,j))
        ans.append((lst[-1],lst[0]))

print(len(ans))
for i,j in ans:
    print(i,j)
0