Given a connected multigraph, output a spanning tree minimizing the number of vertices whose degree differs from the original, following a prescribed construction procedure.
Hard8GraphGreedyTreeSortingNo attempts yetTime limit2sMemory limit1024 MBA new kind of wireless communication with unbounded bandwidth has passed its tests, and it can replace the fiber network that no longer keeps up with traffic growth. You decide the layout of the replacement.
The existing network is a set of nodes that route messages, together with fiber links, each joining two different nodes. Between every pair of nodes there is at least one route along the fiber, and a pair of nodes may be joined by more than one link for bandwidth reasons.
The new network has no fiber. It has wireless links, each joining two nodes. Bandwidth is unbounded, but a wireless link is expensive, so the new network keeps everything connected with as few links as possible: between every pair of nodes there must be exactly one route along the wireless links. Each node was also built with a particular number of connections in mind. A node that ends up connected to a different number of links than it has today must be reorganized, and that costs money.
Design the new network so that exactly one path joins each pair of nodes and the number of nodes whose link count differs from the existing network is as small as possible.
The first line contains two integers n and m (2≤n≤104, 1≤m≤105), the number of nodes and the number of fiber links in the existing network. The nodes are numbered from 0 to n−1.
Each of the next m lines contains two distinct integers ai and bi, meaning that the i-th fiber link joins node ai and node bi. For every pair of nodes there is at least one path of fiber links connecting them. A pair of nodes may be joined by more than one fiber link.
On the first line print the smallest number of nodes whose number of links has to change.
On the second line print the number of nodes, which equals the number in the input, and the number of wireless links. On each of the remaining lines print one wireless link as two node numbers with the smaller number first. Sort the links in ascending order, first by the smaller number and then by the larger one.
Many layouts reach the minimum, so print the one that this procedure builds. Let dv be the number of fiber links at node v in the existing network, counting repeated links between the same pair separately.
Step 3 always places every uj, and the number of wireless links is always n−1.