結果
| 問題 | No.583 鉄道同好会 | 
| コンテスト | |
| ユーザー |  convexineq | 
| 提出日時 | 2021-03-04 16:32:45 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 195 ms / 2,000 ms | 
| コード長 | 996 bytes | 
| コンパイル時間 | 215 ms | 
| コンパイル使用メモリ | 82,148 KB | 
| 実行使用メモリ | 106,880 KB | 
| 最終ジャッジ日時 | 2024-10-05 01:14:42 | 
| 合計ジャッジ時間 | 2,682 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 16 | 
ソースコード
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")
            
            
            
        