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 Rust (1.15.1) (judge verdict: Accepted)

use std::io::{BufRead, Write};
use std::io;
use std::collections::HashSet;
use std::iter;

fn pop(remaining: &mut HashSet<usize>, adjacency: &Vec<Vec<usize>>, current: usize) -> Vec<usize> {
    for i in &adjacency[current] {
        if remaining.remove(i) {
            let mut result = pop(remaining, adjacency, *i);
            result.push(*i);
            return result;
        }
    }
    return Vec::new();
}

fn solve<R: BufRead, W: Write>(mut input: R, mut output: W) {
    let mut s = String::new();
    input.read_line(&mut s).unwrap();
    let mut split = s.split_whitespace();
    let n = split.next().unwrap().parse::<usize>().unwrap();
    let m = split.next().unwrap().parse::<usize>().unwrap();
    let mut adjacency = vec![Vec::<usize>::new();n];
    for _ in 0..m {
        let mut s = String::new();
        input.read_line(&mut s).unwrap();
        let mut split = s.split_whitespace();
        let a = split.next().unwrap().parse::<usize>().unwrap() - 1;
        let b = split.next().unwrap().parse::<usize>().unwrap() - 1;
        adjacency[a].push(b);
        adjacency[b].push(a);
    }
    let mut remaining = (0..n).collect::<HashSet<_>>();
    remaining.remove(&0);
    let after = pop(&mut remaining, &adjacency, 0);
    let before = pop(&mut remaining, &adjacency, 0);
    let result = before
        .into_iter()
        .chain(iter::once(0))
        .chain(after.into_iter().rev())
        .map(|x| (x + 1).to_string())
        .collect::<Vec<_>>();
    writeln!(output, "{}\n{}", result.len(), result.join(" ")).unwrap();
}

fn main() {
    let stdin = io::stdin();
    let stdout = io::stdout();
    solve(stdin.lock(), stdout.lock());
}

#[cfg(test)]
mod tests {
    fn test(input: &str, output: &str) {
        let mut v = Vec::<u8>::new();
        ::solve(input.as_bytes(), &mut v);
        assert_eq!(String::from_utf8(v).unwrap(), output);
    }
    #[test]
    fn case1() {
        test("5 6
1 3
1 4
2 3
1 5
3 5
2 4\n",
             "4
2 3 1 4\n");
    }
    #[test]
    fn case2() {
        test("7 8
1 2
2 3
3 4
4 5
5 6
6 7
3 5
2 6\n",
             "7
1 2 3 4 5 6 7\n");
    }
    #[test]
    fn case3() {
        test("12 18
3 5
4 12
9 11
1 10
2 5
6 10
8 11
1 3
4 10
2 4
3 7
2 10
3 12
3 9
1 7
2 3
2 11
10 11\n",
             "8
12 4 2 5 3 9 11 8\n");
    }
}