Build a tree
Time limit2sMemory limit512 MB
Construct a tree on n nodes with exactly m leaves whose sorted edge list is lexicographically smallest, and print its n-1 edges.
- Level
Medium4 of 10
- Topics
- Tree, Greedy, Implementation, Brute force
- Solved
- No attempts yet
Problem
You are given and . Build a tree that has nodes and exactly leaves, then print it.
A tree is a connected graph with no cycle, and a leaf is a node of degree . The nodes are numbered through .
Several trees can satisfy the condition. Print the one whose edge list comes first in lexicographic order. Build the edge list this way: write each edge as with the smaller endpoint first, so , then order the edges by ascending, breaking ties by ascending. Compare two edge lists as sequences of integers.
Under the given limits such a tree always exists.
Input
The first line contains and , separated by a space. (, )
Output
Print the edges of the tree, one per line, in the order defined above. On each line print the two endpoints of the edge, smaller number first, separated by a space.