Cod sursa(job #2449877)

Utilizator voyagerSachelarie Bogdan voyager Data 20 august 2019 22:14:23
Problema Ciclu Eulerian Scor 40
Compilator py Status done
Runda Arhiva educationala Marime 1.01 kb
#!/usr/bin/env python3

import sys
sys.stdout = open('ciclueuler.out', 'w')

class Node(object):
    def __init__(self, lbl):
        self.lbl = lbl
        self.edges = []
        self.deg = 0

with open('ciclueuler.in', 'r') as f:
    pair = lambda: tuple(map(int, f.readline().split()))

    N, M = pair()
    nodes, ans, visEdge = [Node(str(i + 1)) for i in range(N)], [], [False] * M
    for i in range(M):
        A, B = pair()
        nodes[A - 1].edges.append((nodes[B - 1], i))
        nodes[B - 1].edges.append((nodes[A - 1], i))

    if any(len(node.edges) & 1 for node in nodes):
        print(-1)
    else:
        stk = [nodes[0]]
        while stk:
            v, hasNeighb = stk[-1], False
            while v.edges:
                vv, e = v.edges.pop()
                if visEdge[e]:
                    continue
                stk.append(vv)
                visEdge[e] = hasNeighb = True
                break

            if not hasNeighb:
                ans.append(stk.pop().lbl)
        print(' '.join(ans))