Subtask 1: enumerate all subsets of the possible edges. Check the edge bound, connectivity, and number of odd-degree vertices. This takes time and space.
Full solution: the sum of degrees equals twice the number of edges and is therefore even. Hence an odd is impossible. This also handles , since then .
If is even, then . First connect vertex to every vertex . Vertex has odd degree , and the other vertices have degree . Exactly vertices have odd degree.
Attach the remaining vertices as a path starting at vertex : add . Each added vertex changes the old endpoint's degree from to and creates a new endpoint of degree , preserving the number of odd-degree vertices.
The result is a connected tree without loops or multiple edges. It has edges, satisfying the output bound. If , no path edges are added.
The solution takes time and extra space, excluding output buffers. The checker uses an ordered set for duplicate edges and a disjoint-set structure for connectivity, taking time and space. Since , all arithmetic fits in signed 64-bit integers.
Solution written by GPT