Ghost
Time limit1sMemory limit1024 MB
Given a tree plus a ring road through its leaves, build a tree decomposition with at most 4N bags of size 4 or less, each vertex's bags connected.
- Level
Hard9 of 10
- Topics
- Graph, Tree, Implementation
- Solved
- No attempts yet
Problem
A ghost is haunting South Korea. The ghost of KOI 2022 Round 2...
The KOI city has intersections and bidirectional roads, and any two distinct intersections can reach each other using only roads. In other words, the road network is a tree. The roads lie on a two-dimensional plane, and no two roads cross except at their endpoints.
Until a few decades ago, KOI city was a small town. As people moved in, it grew quickly. During this rapid expansion, the mayor numbered the intersections from to for administrative convenience. The numbering has the following properties.
- Intersection is the city center, and it is adjacent to at least roads.
- The numbers form one of the preorder traversals of the tree rooted at intersection .
- For every intersection, look at the adjacent intersections (directly connected by a road) and pick the one with the smallest number. Starting from that intersection, list the numbers of the adjacent intersections in counterclockwise order. The numbers increase.
As more people arrived, traffic congestion got worse. To deal with it, the mayor connected the intersections with the weakest transport links, meaning those adjacent to exactly one road, with an outer ring road. Let be the numbers of the intersections adjacent to exactly one road, sorted in increasing order. For every , the mayor built a bidirectional road between intersection and intersection . Because of the numbering, the ring roads can be added so that no two roads cross except at their endpoints.
Workers do not want to turn a tree into a graph. Congestion relief shortens commutes, and capitalists then find it easier to exploit workers. Workers want to return to the good old days when HLD and centroid decomposition could be done on the KOI city tree. First, they plan to overthrow the capitalists in a socialist revolution. Then they want to build a new tree as follows, to rebuild the structure of the original KOI city.
- The new tree has at most vertices. If the new tree has vertices, number them .
- Vertex of the new tree has a set , which is a subset of .
- For every road of the original KOI city, there is some set that contains both and . Roads here include the original tree roads and the outer ring roads.
- For every , let be the set of new tree vertices with . Each must be nonempty and revolutionary.
- For every , must hold. (Solutions with may receive partial credit.)
A vertex subset of the new tree is revolutionary if the following holds. For any two distinct vertices and in , if and can be connected by walking through the new tree using only vertices of , then we say and are "connected within ". is revolutionary when every pair of distinct vertices in is always connected within .
For example, consider the following new tree.

If , then , , and are each connected within . However, and are not connected within .
Given the road network of KOI city, output the new tree and the set of each vertex.
Input
The first line contains , the number of intersections in KOI city.
The next lines follow. The -th of these lines contains one integer , which means there is a bidirectional road between intersection and intersection .
The roads given in the input are not the outer ring roads.
Output
On the first line, print , the number of vertices in the new tree. It must satisfy .
Next, print lines. The first integer on the -th line is the size of , followed by the distinct integers of , separated by spaces.
Then print lines describing the edges of the new tree, in order.
A valid answer always exists.
Constraints
Hint
may be the empty set.
If there were no size limit on , a trivial answer would exist: and .