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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
class Program
{
    static IO io = new IO();

    static List<int>[] xPointCanMove;

    static void Main(string[] args)
    {
        //入力
        io.ReadSplit();
        int N = io.Int();
        int M = io.Int();
        int[] A = new int[M];
        int[] B = new int[M];
        for (int i = 0; i < M; ++i)
        {
            io.ReadSplit();
            A[i] = io.Int();
            B[i] = io.Int();
        }

        //計算
        xPointCanMove = new List<int>[N + 1];
        for (int i = 0; i < N + 1 ; ++i)
        {
            xPointCanMove[i] = new List<int>();
        }
        for (int i = 0; i < M; ++i)
        {
            xPointCanMove[A[i]].Add(B[i]);
            xPointCanMove[B[i]].Add(A[i]);
        }

        int[] xPath = GetPath1();

        //出力
        io.Write(xPath.Length.ToString());
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        for (int i =0; i< xPath.Length - 1; ++i)
        {
            sb.Append(xPath[i].ToString());
            sb.Append(" ");
        }
        sb.Append(xPath[xPath.Length - 1].ToString());
        io.Write(sb.ToString());
    }
    static int[] GetPath1()
    {
        int[] xPath = new int[0];
        for (int i = 1; i <= xPointCanMove.Length; ++i)
        {
            int[] xMustPoints = xPointCanMove[i].ToArray();
            xPath = GetPath(xPath, 0, i, xMustPoints);
            if (xPath.Length > 1)
            {
                return xPath;
            }
        }
        return new int[0];
    }
    static int[] GetPath(int[] xPaths, int xLength, int xPoint, int[] xMustPoints)
    {
        if (IndexOfValueInArray(xPaths, xPoint) >= 0)
        {
            return new int[0];
        }

        int[] xNewPaths = new int[xLength + 1];
        Array.Copy(xPaths, xNewPaths, xPaths.Length);
        xNewPaths[xNewPaths.Length - 1] = xPoint;

        int[] xNewMustPoints = new int[xMustPoints.Length];
        Array.Copy(xMustPoints, xNewMustPoints, xMustPoints.Length);

        int xIndexOfValueInArray = IndexOfValueInArray(xMustPoints, xPoint);
        if (xIndexOfValueInArray >= 0)
        {
            xNewMustPoints = RemoveValueFromArray(xNewMustPoints, xIndexOfValueInArray);
            if (xNewMustPoints.Length == 0)
            {
                return xNewPaths;
            }
        }
        foreach (int xNewPoint in xPointCanMove[xPoint])
        {
            int[] xNewNewPaths = GetPath(xNewPaths, xLength + 1, xNewPoint, xNewMustPoints);
            if (xNewNewPaths.Length > 0)
            {
                return xNewNewPaths;
            }
        }
        return new int[0];
    }

    static int[] RemoveValueFromArray(int[] xArray, int xIndex)
    {
        int[] xNewArray = new int[xArray.Length - 1];
        for (int i = 0; i < xIndex - 1; ++i)
        {
            xNewArray[i] = xArray[i];
        }
        for (int i = xIndex + 1; i < xArray.Length; ++i)
        {
            xNewArray[i - 1] = xArray[i];
        }
        return xNewArray;
    }
    static int IndexOfValueInArray(int[] xArray, int xValue)
    {
        for (int i = 0; i < xArray.Length; ++i)
        {
            if (xArray[i] == xValue) { return i; }
        }
        return -1;
    }

}

class IO
{
    private string[] input;
    private int index;
    public void Read()
    {
        input = new string[1];
        input[0] = Console.ReadLine();
        index = -1;
    }
    public void ReadSplit()
    {
        input = Console.ReadLine().Split(' ');
        index = -1;
    }
    public string String()
    {
        index += 1;
        return input[index];
    }
    public string[] String(int count)
    {
        index += 1;
        string[] rv = new string[count];
        Array.Copy(input, index, rv, 0, count);
        return rv;
    }
    public int Int()
    {
        index += 1;
        return int.Parse(input[index]);
    }
    public int[] Int(int count)
    {
        index += 1;
        int[] rv = new int[count];
        for (int i = 0; i < count; ++i)
        {
            rv[i] = int.Parse(input[index + i]);
        }
        return rv;
    }
    public long Long()
    {
        index += 1;
        return long.Parse(input[index]);
    }
    public long[] Long(int count)
    {
        index += 1;
        long[] rv = new long[count];
        for (int i = 0; i < count; ++i)
        {
            rv[i] = long.Parse(input[index + i]);
        }
        return rv;
    }
    public void Write(string str)
    {
        Console.WriteLine(str);
    }
}