Trees
Time limit1sMemory limit256 MB
Given N and K, build a rooted tree where every internal node has exactly K children, or report impossible, and output the lexicographically smallest edge-list string where space sorts before digits.
- Level
Medium7 of 10
- Topics
- Tree, Greedy, Implementation, Math
- Solved
- No attempts yet
Problem
Vasya has gone deep into graph theory. He read a chapter about trees, and one problem keeps bothering him: he must build a rooted tree with nodes in which every node except the leaves has exactly children. The answer is written as a list of edges, and among all valid trees he must find the lexicographically minimal one.
The list of edges is written into a string as follows. Each edge is described by a pair of integers, the numbers of the nodes it connects. The two numbers are written without leading zeroes, with exactly one space character between them. The string consists of the descriptions of all edges, written consecutively and separated by a single space character. All nodes are numbered from to , and the root is node .
Vasya must find the lexicographically minimal string that can be produced this way for a rooted tree of the required kind. In lexicographic comparison, the space character is smaller than every digit.
For example, consider a tree with nodes in which every non-leaf node has children. A tree with edges satisfies the requirement. Its edge list can be written as a string in several ways:
4 2 4 3 1 4 1 52 4 3 4 1 4 1 51 4 1 5 2 4 3 4
Each of these is smaller than the previous one, but none is optimal. For these values of and , the lexicographically minimal string 1 2 1 3 2 4 2 5 comes from a different tree.
Help Vasya solve this task; he has a graph theory test coming up!
Input
The first line of the input file contains two integers and , where is the number of nodes in the required tree and is the number of children of each non-leaf node (, ).
Output
If no tree with the specified parameters exists, print the word No on the only line of the output file.
Otherwise, print the word Yes on the first line of the output file, and print the required lexicographically minimal string on the second line.