結果

問題 No.1553 Lovely City
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2021-06-19 15:53:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 936 ms / 2,000 ms
コード長 1,709 bytes
コンパイル時間 394 ms
コンパイル使用メモリ 86,760 KB
実行使用メモリ 140,540 KB
最終ジャッジ日時 2023-09-05 00:56:12
合計ジャッジ時間 22,535 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
71,764 KB
testcase_01 AC 93 ms
71,660 KB
testcase_02 AC 109 ms
84,800 KB
testcase_03 AC 99 ms
72,224 KB
testcase_04 AC 101 ms
72,368 KB
testcase_05 AC 108 ms
76,804 KB
testcase_06 AC 110 ms
76,596 KB
testcase_07 AC 104 ms
76,408 KB
testcase_08 AC 715 ms
119,156 KB
testcase_09 AC 745 ms
116,924 KB
testcase_10 AC 765 ms
118,164 KB
testcase_11 AC 674 ms
112,864 KB
testcase_12 AC 810 ms
121,040 KB
testcase_13 AC 663 ms
107,540 KB
testcase_14 AC 717 ms
115,208 KB
testcase_15 AC 714 ms
113,244 KB
testcase_16 AC 735 ms
116,792 KB
testcase_17 AC 687 ms
113,876 KB
testcase_18 AC 909 ms
128,976 KB
testcase_19 AC 926 ms
138,388 KB
testcase_20 AC 925 ms
139,644 KB
testcase_21 AC 924 ms
129,036 KB
testcase_22 AC 919 ms
128,000 KB
testcase_23 AC 915 ms
129,136 KB
testcase_24 AC 930 ms
128,816 KB
testcase_25 AC 936 ms
140,540 KB
testcase_26 AC 932 ms
134,948 KB
testcase_27 AC 913 ms
130,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
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())
uf = Unionfind(n+1)
e = [[] for i in range(n+1)]
deg = [0]*(n+1)
use = [0]*(n+1)
for _ in range(m):
    u,v = map(int,input().split())
    e[u].append(v)
    uf.union(u,v)
    use[u] = 1
    use[v] = 1
    deg[v] += 1
group = [[] for i in range(n+1)]
for i in range(n+1):
    if use[i]:
        group[uf.find(i)].append(i)
ans = []
for i in range(n+1):
    if group[i] == []:
        continue
    q = deque([])
    loop = 0
    for ind in group[i]:
        if deg[ind]:
            continue
        q.append(ind)
    order = []
    while q:
        now = q.pop()
        order.append(now)
        for nex in e[now]:
            deg[nex] -= 1
            if deg[nex] == 0:
                q.append(nex)
    if len(order) == len(group[i]):
        for x,y in zip(order,order[1:]):
            ans.append((x,y))
    else:
        for j in range(len(group[i])):
            x = group[i][j]
            y = group[i][(j+1)%len(group[i])]
            ans.append((x,y))
print(len(ans))
for x,y in ans:
    print(x,y)
0