Trees and Path Lengths 2
Time limit2sMemory limit512 MB
Find leaf counts p, q, r on a fixed 4-vertex path so the tree has exactly S length-3 paths, minimizing N then (p,q,r).
- Level
Medium5 of 10
- Topics
- Math, Implementation, Brute force, Combinatorics
- Solved
- No attempts yet
Problem
You are given an integer . Build a tree that has exactly simple paths of length 3. The number of vertices must be at least 1 and at most 500.
A simple path never visits the same vertex twice, and its length is the number of edges on it. Direction does not matter, so A-B-C-D and D-C-B-A are the same path.
Many trees satisfy the condition, so the shape of the tree you print is restricted as follows and the answer becomes unique.
The vertices are numbered 0 to . Start from the path formed by the edges (0, 1), (1, 2), (2, 3), then attach leaves to it. Only vertices 0, 1 and 3 can receive leaves, and they receive , and leaves respectively (). The new leaves are numbered from 4 upward: first the leaves of vertex 0, then the leaves of vertex 1, then the leaves of vertex 3. So .
Among the trees of this shape that have exactly simple paths of length 3, print the one with the smallest . If several trees share that smallest , print the one whose triple is lexicographically smallest. Such a tree exists for every in the given range, and its is always at most 500.
Input
The first line contains . ()
Output
Print the number of vertices on the first line.
On each of the next lines, print the two endpoints of one edge, separated by a space. Print the edges (0, 1), (1, 2), (2, 3) first, in that order, then the leaf edges in increasing order of leaf number. In a leaf edge, write the number of the vertex the leaf is attached to first and the number of the leaf second.