eolymp
bolt
Try our new interface for solving problems
Problems

Match them up

Match them up

This time we'll have to search for maximal pair matching. And not simple, but lexicographically minimal. You are given a bipartite graph of \textbf{N} vertices in the left set, \textbf{N} vertices in the right set and \textbf{K} edges between them. Maximal pair matching is subset of edges of graph \textbf{M} with maximal power, such that any vertex of graph is incedent to no more than one edge from \textbf{M}. Let \{(\textbf{u_i}, \textbf{v_i})\} set of edges be maximal pair matching, where \textbf{u_i} are vertices from left set and \textbf{v_i} are verticesfrom right set. Write all the vertices into one consequence in order: \textbf{u_1}, \textbf{v_1}, \textbf{u_2}, \textbf{v_\{2 \}},..., \textbf{u_m}, \textbf{v_m}. Find maximal pair matching with lexicographically minimal consequence of vertices. \InputFile In the first line two numbers \textbf{N} and \textbf{K} are amounts of vertices in each set and amount of edges. Following \textbf{K} lines contain edges de nition. Each edge is de ned with a pair of numbers \textbf{a_i} and \textbf{b_i}, where \textbf{a_i} is a vertex from left set, and \textbf{b_i} is a vertex from right set. \OutputFile Output size of maximal pair matching \textbf{m}. in the first line. Then write \textbf{m} lines with two numbers \textbf{u_i} and \textbf{v_i} each, where \textbf{u_i} is vertex from left set, and \textbf{v_i} is vertex from right set, corresponding to \textbf{i}th edge of the match. \textbf{Limits} \textbf{1} ≤ \textbf{N} ≤ \textbf{10^3} \textbf{0} ≤ \textbf{K} ≤ \textbf{10^5} \textbf{1} ≤ \textbf{a_i}, \textbf{b_i} ≤ \textbf{N}
Time limit 1.5 second
Memory limit 256 MiB
Input example #1
3 5
1 2
1 3
3 2
2 3
2 1
Output example #1
3
1 3
2 1
3 2