結果

問題 No.1553 Lovely City
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2021-06-18 22:30:55
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,772 bytes
コンパイル時間 404 ms
コンパイル使用メモリ 87,064 KB
実行使用メモリ 715,268 KB
最終ジャッジ日時 2023-09-04 23:42:31
合計ジャッジ時間 34,727 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,176 KB
testcase_01 AC 72 ms
71,624 KB
testcase_02 AC 152 ms
112,828 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 79 ms
75,616 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 TLE -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 TLE -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
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
input = sys.stdin.readline
sys.setrecursionlimit(1000000)

class SCC:

    def __init__(self, n):
        self.n = n
        self.graph = [[] for i in range(n)]
        self.graph_rev = [[] for i in range(n)]
        self.used = [False]*n

    def add_edge(self, fr, to):
        if fr == to:
            return
        self.graph[fr].append(to)
        self.graph_rev[to].append(fr)

    def dfs(self, node, graph):
        self.used[node] = True
        for nex in graph[node]:
            if self.used[nex]:
                continue
            self.dfs(nex,graph)
        self.order.append(node)

    def first_dfs(self):
        self.used = [False]*self.n
        self.order = []
        for i in range(self.n):
            if self.used[i]:
                continue
            self.dfs(i,self.graph)
    
    def second_dfs(self):
        self.used = [False]*self.n
        self.ans = []
        for node in reversed(self.order):
            if self.used[node]:
                continue
            self.used[node] = True
            self.order = []
            self.dfs(node, self.graph_rev)
            self.ans.append(self.order)

    def scc(self):
        self.first_dfs()
        self.second_dfs()
        return self.ans


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())
cand = set()
uf = Unionfind(n)
scc = SCC(n)
deg = [0]*n
for _ in range(m):
    u,v = map(int,input().split())
    u,v = u-1,v-1
    uf.union(u,v)
    scc.add_edge(u,v)
    cand.add(u)
    cand.add(v)
    deg[u] += 1
ok = 0
for i in cand:
    if deg[i]:
        continue
    ok = 1

if ok == 0:
    l = list(cand)
    ans = []
    for i in range(len(l)):
        x = l[i]
        y = l[(i+1)%len(l)]
        ans.append((x,y))
    print(len(ans))
    for x,y in ans:
        print(x+1,y+1)
    exit()

group = scc.scc()
group.append([])
ans = []
for x,y in zip(group,group[1:]):
    if len(x) != 1:
        for i in range(len(x)):
            a = x[i]
            b = x[(i+1)%len(x)]
            ans.append((a,b))
    if y == []:
        continue
    if uf.same(x[0],y[0]):
        ans.append((x[0],y[0]))
print(len(ans))
for x,y in ans:
    print(x+1,y+1)
0