結果

問題 No.583 鉄道同好会
ユーザー convexineqconvexineq
提出日時 2021-03-04 11:22:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 455 ms / 2,000 ms
コード長 1,117 bytes
コンパイル時間 314 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 202,496 KB
最終ジャッジ日時 2024-04-15 04:32:31
合計ジャッジ時間 2,320 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
51,840 KB
testcase_01 AC 35 ms
52,224 KB
testcase_02 AC 38 ms
52,352 KB
testcase_03 AC 35 ms
52,224 KB
testcase_04 AC 36 ms
52,224 KB
testcase_05 AC 34 ms
52,224 KB
testcase_06 AC 35 ms
52,352 KB
testcase_07 AC 35 ms
52,096 KB
testcase_08 AC 35 ms
52,096 KB
testcase_09 AC 35 ms
52,224 KB
testcase_10 AC 37 ms
53,632 KB
testcase_11 AC 74 ms
77,568 KB
testcase_12 AC 87 ms
79,104 KB
testcase_13 AC 86 ms
78,976 KB
testcase_14 AC 88 ms
78,976 KB
testcase_15 AC 90 ms
79,360 KB
testcase_16 AC 120 ms
85,248 KB
testcase_17 AC 131 ms
85,444 KB
testcase_18 AC 455 ms
202,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def find_Eulerian_path_undirected(s,g,edge_valid):
    def dfs(v):
        while g[v]:
            u,idx = g[v].pop()
            if edge_valid[idx]:
                edge_valid[idx] -= 1
                dfs(u)
        path.append(v)
    path = []
    dfs(s)
    return path[::-1]

def Eulerian_path_undirected(g,m):
    n = len(g)
    edge_valid = [1]*m
    deg = [len(gi)%2 for gi in g]
    used = [int(gi == []) for gi in g] # 辺が出ていない頂点は見ない
    cnt = deg.count(1)
    if cnt > 2:
        return False
    elif cnt == 2:
        s = deg.index(1)
    elif cnt == 0:
        s = used.index(0)
    
    path = find_Eulerian_path_undirected(s,g,edge_valid)
    if path == False: return False
    for v in path:
        used[v] = 1
    if any(ui==0 for ui in used): return False
    else: return path

import sys
sys.setrecursionlimit(10**6)
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_path_undirected(g,m)
if path == False:
    print("NO")
else:
    print("YES")
0