Problem:
Score : 500 points

Problem Statement

You are given a connected undirected simple graph, which has N vertices and M edges.
The vertices are numbered 1 through N, and the edges are numbered 1 through M.
Edge i connects vertices A_i and B_i.
Your task is to find a path that satisfies the following conditions:

The path traverses two or more vertices.

The path does not traverse the same vertex more than once.

A vertex directly connected to at least one of the endpoints of the path, is always contained in the path.

It can be proved that such a path always exists.
Also, if there are more than one solution, any of them will be accepted.

Constraints

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i < B_i \leq N

The given graph is connected and simple (that is, for every pair of vertices, there is at most one edge that directly connects them).

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
:
A_M B_M

Output

Find one path that satisfies the conditions, and print it in the following format.
In the first line, print the count of the vertices contained in the path.
In the second line, print a space-separated list of the indices of the vertices, in order of appearance in the path.

Sample Input 1

5 6
1 3
1 4
2 3
1 5
3 5
2 4

Sample Output 1

4
2 3 1 4

There are two vertices directly connected to vertex 2: vertices 3 and 4.
There are also two vertices directly connected to vertex 4: vertices 1 and 2.
Hence, the path 2 → 3 → 1 → 4 satisfies the conditions.

Sample Input 2

7 8
1 2
2 3
3 4
4 5
5 6
6 7
3 5
2 6

Sample Output 2

7
1 2 3 4 5 6 7

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.*;
import java.util.*;

public class Main {

    static ArrayList<Integer> adjList[];
    static int n;
    static boolean visited[];
    static ArrayList<Integer> res = new ArrayList<>();

    static void dfs(int node) {
        visited[node] = true;
        res.add(node);

        for (int child : adjList[node])
            if (!visited[child]) {
                dfs(child);
                break;
            }
    }

    static int s;

    static boolean dfs2(int node) {
        visited[node] = true;
        if (node == s) {
            res.add(node);
            return true;
        }
        boolean ok = false;
        for (int child : adjList[node]) {
            if (!visited[child]) {
                if (dfs2(child)) {
                    res.add(node);
                    ok = true;
                    break;
                }
            }
        }
        return ok;
    }

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        n = sc.nextInt();
        adjList = new ArrayList[n];
        visited = new boolean[n];
        for (int i = 0; i < n; i++)
            adjList[i] = new ArrayList<>();

        int m = sc.nextInt();
        while (m-- > 0) {
            int a = sc.nextInt() - 1, b = sc.nextInt() - 1;
            adjList[a].add(b);
            adjList[b].add(a);
        }

        boolean found = false;
        for (int i = 0; i < n; i++) {
            if (adjList[i].size() == 1) {
                found = true;
                dfs(i);
                break;
            }
        }

        if (!found) {
            for (int i = 0; i < n; i++) {
                if (adjList[i].size() == 2) {
                    res.add(i);
                    s = adjList[i].get(1);
                    dfs2(adjList[i].get(0));
                    break;
                }
            }
        }
        out.println(res.size());
        for (int i = 0; i < res.size(); i++) {
            if (i > 0) out.print(" ");
            out.print(res.get(i) + 1);
        }
        out.println();
        out.flush();
        out.close();
    }


    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public Scanner(FileReader f) {
            br = new BufferedReader(f);
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++)
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec)
                        f *= 10;
                }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() throws IOException {
            return br.ready();
        }

    }
}