"Construction problem greedy" cf1041e. Tree reconstruction

Source: Internet
Author: User

The game is still too slow ... If you can do it quickly, you can score.

Monocarp have drawn a tree (an undirected connected acyclic graph) and then have given each vertex an index. All indices is distinct numbers from11 to  N ">nn. For every edge  e " >ee  of this tree, Monocarp have written, numbers:the maximum indices of the vertices of the "the" formed if the ED Ge  e ">ee  (and only this edge) are erased from the tree.

Monocarp have given you a list of n−1n−1 pairs of numbers. He wants provide an example of a tree that would produce the said list if this tree exists. If Such tree does not exist, say so.

Input

The first line contains one integer nn (2≤n≤10002≤n≤1000)-the number of vert Ices in the tree.

Each of the nextN−1n−1 lines contains, integersAIAI andBIbi each (1≤ai<bi≤n1≤ai<bi≤n)-the maximal indices of vertices in the components Formed if the ii-th edge is removed.

Output

If There is no such tree that can produce the given list of pairs, print "no" (without quotes).

Otherwise print "YES" (without quotes) in the first line and the edges of the tree in the nextN−1n−1 lines. Each of the lastN−1n−1 lines should contain, integersXIxi andy iyi  ( 1 & #x2264; x i , y i & #x2264; N " >1≤xi yi≤n1≤xi,yi≤n)  -vertices connected by an edge.

Note:the numeration of edges doesn ' t matter for this task. Your solution would be considered correct if Your tree produces the same pairs as given in the input file (possibly reorder ED). That's means that's you can print the edges of the tree to reconstructed in any order.

Main topic

Existing a tree. Describe each edge of the tree with a $n-1$ two-tuple (x, Y), indicating that the maximum number of the two connected blocks is the largest one, after deleting the edge.

Ask if there is a tree that fits the description.

Topic Analysis Chrysanthemum Diagram construction

Notice that when the tree is divided into two connected blocks, the resulting two-tuple (x, y) must have an element of $n$.

So, how many two-tuple $ (x, y) (y=n) $ will explain how many edges are satisfied: $[x+1,n]$ these points and $x$ points respectively in the two pieces that were cut.

A tree has a property: The path between two points is unique. So in order to sever $x$ and $[x+1]...n$, the two connected blocks can only have unique paths, and the points on the path must be less than $x$. So here is a method for constructing a single point $x$. So what should other points consider? Is it a fork or a new one that should be done on the path?

Structural problems can be divided into two categories: the only solution and many solutions. This multi-solution problem, of course, choose a most concise method of construction. As a matter of fact, the way to construct a daisy chart is legal for a single point to go on.

Our concern about the construction of chrysanthemums is that if you open a new path each time, will you waste some points? In other words, the original path can not be shared with some points?

However, the edges on the shared path only contribute to a single tuple anyway. If shared, in order to reach a given number of two tuples, it is only possible to insert a separate full path on the original path ———— that is, the equivalent is not shared.

1#include <bits/stdc++.h>2 Const intMAXN =1003;3 4 intN;5 BOOLUSED[MAXN];6 intMP[MAXN][MAXN];7 intedgetot,edges[maxn<<1],nxt[maxn<<1],HEAD[MAXN];8 9 intRead ()Ten { One     CharCH =GetChar (); A     intnum =0; -     BOOLFL =0; -      for(;!isdigit (CH); ch=GetChar ()) the         if(ch=='-') FL =1; -      for(; isdigit (CH); ch=GetChar ()) -num = (num<<1) + (num<<3) +ch- -; -     if(fl) num =-num; +     returnnum; - } + voidErrordown () A { atPuts"NO"); -Exit0); - } - voidAddedge (intUintv) - { -Edges[++edgetot] = V, nxt[edgetot] = Head[u], head[u] =Edgetot; inEdges[++edgetot] = u, Nxt[edgetot] = Head[v], head[v] =Edgetot; - } to voidDfsintXintFA) + { -      for(intI=HEAD[X]; i!=-1; I=Nxt[i]) the     { *         intv =Edges[i]; $         if(V!=FA) printf ("%d%d\n", X,v), DFS (v, x);Panax Notoginseng     } - } the intMain () + { An =read (); theMemset (Head,-1,sizeofhead); +      for(intI=1; i<n; i++) -     { $         intU = Read (), V =read (); $         if(U >v) std::swap (U, v); -         if(v!=N) errordown (); -mp[u][v]++; the     } -      for(intI=1; i<n; i++)Wuyi         if(Mp[i][n]) { the             if(I <Mp[i][n]) errordown (); -Used[i] =1; Wu             intLST = i, cnt =0; -              for(intj=1; j<i&&cnt<mp[i][n]-1; J + +) About                 if(!Used[j]) { $USED[J] =1; - Addedge (LST, j); -LST =J; -cnt++; A                 } +             if(cnt!=mp[i][n]-1) Errordown (); the Addedge (LST, n); -         } $Puts"YES"); theDfs1,0); the     return 0; the}

Chain Construction

The last paragraph of the previous practice did not mean that a legal chain could not be constructed. In fact, it can be found that the small node is deliberately connected to the large node is also possible.

Here is a very ingenious chain construct: The $a_i$ is considered as the maximum value of the prefix, thus constructing a chain.

Specific proofs and codes see the cf1041e "Tree reconstruction"

END

"Construction problem greedy" cf1041e. Tree reconstruction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.