結果

問題 No.1553 Lovely City
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2021-06-19 15:50:03
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,954 bytes
コンパイル時間 448 ms
コンパイル使用メモリ 86,980 KB
実行使用メモリ 141,808 KB
最終ジャッジ日時 2023-09-05 00:55:49
合計ジャッジ時間 22,962 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
71,676 KB
testcase_01 AC 92 ms
71,724 KB
testcase_02 AC 112 ms
86,308 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 110 ms
76,904 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 761 ms
121,196 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 672 ms
115,504 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 #

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 = []
dis = [n+5]*(n+1)
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)
        dis[ind] = 0
    if len(q) == 0:
        for j in range(len(group[i])):
            x = group[j]
            y = group[(j+1)%len(group[i])]
            ans.append((x,y))
        continue
    order = []
    while q:
        now = q.pop()
        order.append(now)
        for nex in e[now]:
            deg[nex] -= 1
            if deg[nex] == 0:
                q.append(nex)
                dis[nex] = dis[now]+1
    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[j]
            y = group[(j+1)%len(group[i])]
            ans.append((x,y))
print(len(ans))
for x,y in ans:
    print(x,y)
0