Independent Edge Set and Certificate

Find a maximum matching and a maximum independent vertex set in a bipartite graph, printing both with lexicographically smallest tie-breaks.

Hard8GraphShortest pathGreedyCombinatoricsNo attempts yetTime limit1sMemory limit512 MB

Problem

Let GG be a simple undirected graph. Two vertices of GG are adjacent if an edge connects them, and two edges of GG are adjacent if they share a vertex. In the graph of Figure 1(a), vertices 33 and 44 are adjacent because the edge (3,4)(3,4) connects them. The edges (3,4)(3,4) and (3,5)(3,5) are adjacent because they share vertex 33.

A subset of the vertices of GG is an independent vertex set if no two of its vertices are adjacent. A subset of the edges of GG is an independent edge set if no two of its edges are adjacent. The size of a largest independent vertex set is the vertex independence number, written α(G)\alpha(G). In the same way, the size of a largest independent edge set is the edge independence number, written ν(G)\nu(G).

Figure 1. Two graphs G1G_1 and G2G_2. (a) The graph G1G_1, where α(G1)=4\alpha(G_1) = 4 and ν(G1)=4\nu(G_1) = 4. (b) The graph G2G_2, where α(G2)=6\alpha(G_2) = 6 and ν(G2)=3\nu(G_2) = 3.

The independent edge set problem asks for a largest independent edge set of a graph. It has been studied for a long time, and several algorithms run in time polynomial in the size of the graph. Still, when a program reads an input XX and prints an answer YY, the user has no way to tell whether YY is correct or has been broken by a bug. A certifying algorithm prints, together with its answer, a certificate ZZ for that answer. The user inspects ZZ by hand or feeds it to a checker program, and then either accepts the answer as correct or discards it as buggy.

Consider what makes a good certificate for the independent edge set problem on a bipartite graph. A graph is bipartite if its vertex set splits into two disjoint sets UU and VV so that every edge joins a vertex of UU to a vertex of VV. Both graphs of Figure 1 are bipartite, with the orange vertices forming one side and the blue vertices the other. König's theorem says that every bipartite graph GG on nn vertices satisfies ν(G)+α(G)=n\nu(G) + \alpha(G) = n. A maximum independent vertex set is therefore a good certificate. Once an independent edge set of size kk and an independent vertex set of size nkn - k have been found in a bipartite graph on nn vertices, ν(G)=k\nu(G) = k and α(G)=nk\alpha(G) = n - k follow, so both sets are as large as possible.

You are given a bipartite graph GG. Find and print a maximum independent edge set YY together with a maximum independent vertex set ZZ, which is the certificate. The vertices are numbered from 11 to nn. A checker confirms that no two edges of YY are adjacent, confirms that no two vertices of ZZ are adjacent, and accepts the answer when Y+Z=n|Y| + |Z| = n.

Input

The first line contains the number of vertices nn and the number of edges mm, separated by a space. (1n10001 \le n \le 1000, 1m500001 \le m \le 50000)

Each of the next mm lines contains one edge, given as the numbers uu and vv of the two vertices it joins, separated by a space. (1u,vn1 \le u, v \le n, uvu \neq v)

The given graph is a simple bipartite graph and is not necessarily connected. No edge is given twice.

Output

On the first line, print the size kk of a maximum independent edge set.

On each of the next kk lines, print one edge of that set. Print the two vertex numbers of an edge in increasing order. Order the lines by the first number, and by the second number when the first numbers are equal. If the graph has several maximum independent edge sets, print the one whose sorted edge list is lexicographically smallest.

On the next line, print the size kk' of the certificate, and on the line after it print the vertices of the certificate in ascending order, separated by single spaces. If the graph has several maximum independent vertex sets, print the one whose ascending vertex list is lexicographically smallest.

Exactly one output satisfies both rules.