結果

問題 No.1553 Lovely City
ユーザー convexineqconvexineq
提出日時 2021-06-18 22:26:31
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,213 bytes
コンパイル時間 301 ms
コンパイル使用メモリ 87,088 KB
実行使用メモリ 152,572 KB
最終ジャッジ日時 2023-09-04 23:38:01
合計ジャッジ時間 20,690 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,252 KB
testcase_01 AC 73 ms
71,068 KB
testcase_02 AC 180 ms
140,924 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 83 ms
75,036 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
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 #

def SCC_Tarjan(g):
    n = len(g)
    order = [-1]*n # 負なら未処理、[0,n) ならpre-order, n ならvisited
    low = [0]*n
    ord_now = 0
    parent = [-1]*n
    gp = [0]*n
    gp_num = 0
    S = []
    q = []
    for i in range(n):
        if order[i] == -1:
            q.append(i)
            while q:
                v = q.pop()
                if v >= 0:
                    if order[v] != -1: continue
                    order[v] = low[v] = ord_now
                    ord_now += 1
                    S.append(v)
                    q.append(~v)
                    for c in g[v]:
                        if order[c] == -1: 
                            q.append(c)
                            parent[c] = v
                        else:
                            low[v] = min(low[v], order[c])
                else:
                    v = ~v
                    if parent[v] != -1:
                        low[parent[v]] = min(low[parent[v]], low[v])
                    if low[v] == order[v]:
                        while True:
                            w = S.pop()
                            order[w] = n
                            gp[w] = gp_num
                            if w==v: break
                        gp_num += 1


    scc = [[] for _ in range(gp_num)]
    for i in range(n):
        gp[i] = gp_num-gp[i]-1
        scc[gp[i]].append(i)
    
    #print(gp)
    return scc


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)
for _ in range(m):
    a,b = map(int,readline().split())
    UF.merge(a-1,b-1)
    g[a-1].append(b-1)

scc = SCC_Tarjan(g)
top = []
last = []
ans = []

bad_root = [0]*n
first_element = [-1]*n

for lst in scc:
    t = lst[0]
    top.append(t)
    last.append(lst[-1])
    r = UF.root(t)
    if first_element[r] == -1:
        first_element[r] = t
    if len(lst) >= 2:
        bad_root[r] = 1
    for i,j in zip(lst,lst[1:]):
        ans.append((i,j))

top.append(-1)
for i,j in zip(last,top[1:]):
    if j != -1 and UF.issame(i,j):
        ans.append((i,j))
    else:
        r = UF.root(i)
        if bad_root[r]:
            ans.append((i,first_element[r]))

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