Buggy python submission (verdict: Time Limit Exceeded):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import defaultdict, deque

    N, M = map(int, readline().split())

    edge = defaultdict(list)

    for _ in range(M):
        a, b = map(int, readline().split())
        edge[a].append(b)
        edge[b].append(a)

    first = 1
    second = edge[1][0]

    visited = [False] * (N + 1)
    visited[first] = True
    visited[second] = True

    def dfs(u):
        path = [u]
        for v in edge[u]:
            if not visited[v]:
                visited[v] = True
                path.extend(dfs(v))
                break
        return path

    res1 = dfs(first)
    res2 = dfs(second)

    print(len(res1) + len(res2))
    print(*res1[::-1], *res2)


if __name__ == '__main__':
    main()


Fixed submission:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import defaultdict, deque

    N, M = map(int, readline().split())

    edge = defaultdict(list)

    for _ in range(M):
        a, b = map(int, readline().split())
        edge[a].append(b)
        edge[b].append(a)

    first = 1
    second = edge[1][0]

    visited = [False] * (N + 1)
    visited[first] = True
    visited[second] = True

    que = deque()
    que.append(first)

    path1 = []
    path2 = []

    flag = True

    while flag:
        u = que.popleft()
        path1.append(u)
        flag = False
        for v in edge[u]:
            if not visited[v]:
                visited[v] = True
                que.append(v)
                flag = True
                break

    que = deque()
    que.append(second)
    flag = True

    while flag:
        u = que.popleft()
        path2.append(u)
        flag = False
        for v in edge[u]:
            if not visited[v]:
                visited[v] = True
                que.append(v)
                flag = True
                break

    print(len(path1) + len(path2))
    print(*path1[::-1], *path2)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import defaultdict, deque

    N, M = map(int, readline().split())

    edge = defaultdict(list)

    for _ in range(M):
        a, b = map(int, readline().split())
        edge[a].append(b)
        edge[b].append(a)

    first = 1
    second = edge[1][0]

    visited = [False] * (N + 1)
    visited[first] = True
    visited[second] = True

    def dfs(u):
        path = [u]
        for v in edge[u]:
            if not visited[v]:
                visited[v] = True
                path.extend(dfs(v))
                break
        return path

    res1 = dfs(first)
    res2 = dfs(second)

    print(len(res1) + len(res2))
    print(*res1[::-1], *res2)


if __name__ == '__main__':
    main()


Fixed submission:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import defaultdict, deque

    N, M = map(int, readline().split())

    edge = defaultdict(list)

    for _ in range(M):
        a, b = map(int, readline().split())
        edge[a].append(b)
        edge[b].append(a)

    first = 1
    second = edge[1][0]

    visited = [False] * (N + 1)
    visited[first] = True
    visited[second] = True

    que = deque()
    que.append(first)

    path1 = []
    path2 = []

    flag = True

    while flag:
        u = que.popleft()
        path1.append(u)
        flag = False
        for v in edge[u]:
            if not visited[v]:
                visited[v] = True
                que.append(v)
                flag = True
                break

    que = deque()
    que.append(second)
    flag = True

    while flag:
        u = que.popleft()
        path2.append(u)
        flag = False
        for v in edge[u]:
            if not visited[v]:
                visited[v] = True
                que.append(v)
                flag = True
                break

    print(len(path1) + len(path2))
    print(*path1[::-1], *path2)


if __name__ == '__main__':
    main()