結果

問題 No.1553 Lovely City
ユーザー ygd.ygd.
提出日時 2021-06-18 22:08:40
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,291 bytes
コンパイル時間 97 ms
コンパイル使用メモリ 10,920 KB
実行使用メモリ 101,232 KB
最終ジャッジ日時 2023-09-04 23:16:00
合計ジャッジ時間 29,955 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,340 KB
testcase_01 AC 16 ms
8,240 KB
testcase_02 AC 17 ms
8,364 KB
testcase_03 WA -
testcase_04 RE -
testcase_05 WA -
testcase_06 RE -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 RE -
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 #

import sys
sys.setrecursionlimit(10**7)

def main():
    N,M = map(int,input().split())
    Q = []
    S = set([])
    for i in range(M):
        U,V = map(int,input().split())
        Q.append((U,V))
        S.add(U)
        S.add(V)
    L = list(S)
    n = len(S) #出てくる頂点の種類
    L.sort()
    dic = {} #元の番号→座圧後の番号
    revdic = {} #座圧後の番号→元の番号
    for i in range(n):
        dic[L[i]] = i
        revdic[i] = L[i]
    ALL = [i for i in range(n)]
    ALL = set(ALL)
    G = [[] for _ in range(n)]
    for u,v in Q:
        uz = dic[u]
        vz = dic[v]
        G[uz].append(vz)
        ALL.discard(vz)
    
    used = set([])

    TS = []
    def dfs(v):
        if v in used:
            return
        used.add(v)
        for w in G[v]:
            if w in used:
                continue
            dfs(w)
            TS.append(w) #帰りがけで記録

    for root in ALL: #すべての始点について
      dfs(root)
      TS.append(root)
    TS.reverse() #最後に反転
    #print(TS)

    print(n-1) #何本か?
    for i in range(n-1):
        s = TS[i]
        t = TS[i+1]
        sori = revdic[s]
        tori = revdic[t]
        ret = [sori, tori]
        print(*ret)


if __name__ == '__main__':
    main()
0