結果

問題 No.583 鉄道同好会
ユーザー convexineqconvexineq
提出日時 2021-03-04 16:32:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 218 ms / 2,000 ms
コード長 996 bytes
コンパイル時間 344 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 107,008 KB
最終ジャッジ日時 2024-04-15 08:39:54
合計ジャッジ時間 3,089 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,096 KB
testcase_01 AC 39 ms
52,480 KB
testcase_02 AC 38 ms
52,096 KB
testcase_03 AC 38 ms
52,224 KB
testcase_04 AC 38 ms
52,352 KB
testcase_05 AC 38 ms
52,224 KB
testcase_06 AC 39 ms
52,192 KB
testcase_07 AC 39 ms
52,352 KB
testcase_08 AC 39 ms
52,608 KB
testcase_09 AC 39 ms
52,480 KB
testcase_10 AC 42 ms
53,632 KB
testcase_11 AC 84 ms
76,864 KB
testcase_12 AC 97 ms
78,972 KB
testcase_13 AC 97 ms
79,132 KB
testcase_14 AC 98 ms
78,984 KB
testcase_15 AC 104 ms
79,104 KB
testcase_16 AC 148 ms
84,884 KB
testcase_17 AC 163 ms
84,352 KB
testcase_18 AC 218 ms
107,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def find_Eulerian_trail_undirected(s,g,m):
    edge_valid = [1]*m
    path = []
    q = [s]
    while q:
        while g[q[-1]]:
            u,idx = g[q[-1]].pop()
            if edge_valid[idx]:
                edge_valid[idx] = 0
                q.append(u)
                break
        else:
            path.append(q.pop())
    return path[::-1]


def Eulerian_trail_undirected(g,m):
    n = len(g)
    deg = [len(gi)%2 for gi in g]
    cnt = deg.count(1)
    if cnt > 2:
        return False
    elif cnt == 2:
        s = deg.index(1)
    elif cnt == 0:
        for s in range(n):
            if g[s]: break
    
    path = find_Eulerian_trail_undirected(s,g,m)
    if len(path) != m+1: return False
    else: return path


import sys
n,m = map(int,input().split())
g = [[] for _ in range(n)]
for i in range(m):
    x,y = map(int,input().split())
    g[x].append((y,i))
    g[y].append((x,i))

path = Eulerian_trail_undirected(g,m)
if path == False:
    print("NO")
else:
    print("YES")
0