Maximum matching P59669


Statement
 

pdf   zip

Given an undirected graph with nn vertices, a matching is a subset of the edges with no common vertices. Write a program to tell if a given graph has a maximum matching, that is, a grouping of the vertices in n/2n/2 pairs such that all vertices belong to some pair, and that both vertices of every pair are directly connected.

Input

Input consists of several cases. Each case begins with nn and the number of edges mm, followed by mm pairs of vertices. Assume 2n202 \le n \le 20, that nn is even, that vertices are numbered from 1 to nn, that there are no repeated edges nor edges connecting a vertex to itself, and that there is no isolated vertex.

Output

For every case, tell if the given graph has a maximum matching.

Observation

There are polynomial-time algorithms, more or less complicated, to solve this problem. Here, we settle for a simple backtracking.

Public test cases
  • Input

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

    Output

    yes
    yes
    no
    no
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Translator
    Salvador Roura
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C++
    User solutions
    C++