結果
| 問題 |
No.1553 Lovely City
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-06-18 22:34:35 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,417 bytes |
| コンパイル時間 | 239 ms |
| コンパイル使用メモリ | 82,256 KB |
| 実行使用メモリ | 171,304 KB |
| 最終ジャッジ日時 | 2024-06-22 21:00:21 |
| 合計ジャッジ時間 | 26,861 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 2 WA * 24 |
ソースコード
import sys
input = sys.stdin.readline
sys.setrecursionlimit(2*10**5)
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())
uf = Unionfind(n)
scc = SCC(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)
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)