Problem:
Score : 800 points

Problem Statement

You are given a simple connected undirected graph consisting of N vertices and M edges.
The vertices are numbered 1 to N, and the edges are numbered 1 to M.

Edge i connects Vertex a_i and b_i bidirectionally.

Determine if three circuits (see Notes) can be formed using each of the edges exactly once.

Notes

A circuit is a cycle allowing repetitions of vertices but not edges.

Constraints

All values in input are integers.

1 \leq N,M \leq 10^{5}

1 \leq a_i, b_i \leq N

The given graph is simple and connected.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
:
a_M b_M

Output

If three circuits can be formed using each of the edges exactly once, print Yes; if they cannot, print No.

Sample Input 1

7 9
1 2
1 3
2 3
1 4
1 5
4 5
1 6
1 7
6 7

Sample Output 1

Yes

Three circuits can be formed using each of the edges exactly once, as follows:

Sample Input 2

3 3
1 2
2 3
3 1

Sample Output 2

No

Three circuits are needed.

Sample Input 3

18 27
17 7
12 15
18 17
13 18
13 6
5 7
7 1
14 5
15 11
7 6
1 9
5 4
18 16
4 6
7 2
7 11
6 3
12 14
5 2
10 5
7 8
10 15
3 15
9 8
7 15
5 16
18 15

Sample Output 3

Yes

Solution in Scala (2.11.7) (judge verdict: Runtime Error)

import scala.annotation.tailrec
import scala.collection.mutable
import scala.io.StdIn._
object Main extends App {

  val Array(n, m) = readLine.trim.split(' ').map(_.toInt)
  val nodes = Array.tabulate(n){_ ⇒ Nil: List[Edge]}
  for (i ← 0 until m){
    val Array(a, b) = readLine.trim.split(' ').map(_.toInt - 1)
    nodes(a) ::= Edge(i, b)
    nodes(b) ::= Edge(i, a)
  }
  println(
    if (dfs(nodes)) "Yes" else "No"
  )

  def dfs(nodes: Array[List[Edge]]): Boolean = {
    val searching = Array.tabulate(n){_ ⇒ false}
    val use = Array.tabulate(m){_ ⇒ false}
    @tailrec
    def sub(prev: Int, current: Int, path: List[Int]): Int= {
      if (searching(current)) {
        path.foreach(i ⇒ searching(i) = false)
        current
      }
      else {
        nodes(current).dropWhile(e ⇒ e.to == prev || use(e.id)) match {
          case h::_ ⇒
            searching(current) = true
            sub(current, h.to, current::path)
          case Nil ⇒
            path.foreach(i => searching(i) = false)
            -1
        }
      }
    }
    def remove(start: Int): Unit = {
      nodes(start).dropWhile(e ⇒ use(e.id)) match {
        case h::t ⇒
          use(h.id) = true
          nodes(start) = t
          sub(h.to)
          nodes(start) = nodes(start).dropWhile(e ⇒ use(e.id))
        case Nil ⇒ ???
      }
      @tailrec
      def sub(current: Int): Unit = {
        if (current != start){
          nodes(current).dropWhile(e ⇒ use(e.id)) match {
            case h::t ⇒
              use(h.id) = true
              nodes(current) = t
              sub(h.to)
            case Nil ⇒ ???
          }
        }
      }
    }
    var count = 0
    for (i ← 0 until n){
      var start = 0
      nodes(i) = nodes(i).dropWhile(e ⇒ use(e.id))
      while(nodes(i).nonEmpty && start != -1){
        start = sub(-1, i, Nil)
        if (start != -1){
          remove(start)
          count += 1
        }
      }
    }
    count >= 3 && use.forall(b ⇒ b)
  }
  case class Edge(id: Int, to: Int)
  implicit class Extension[T](val value: T) extends AnyVal {
    @inline def let[A](@inline func: T ⇒ A): A = func(value)
    @inline def also(@inline func: T ⇒ Unit): T = {func(value); value}
  }
}