981C.md (2753B)
1 Source: https://codeforces.com/problemset/problem/981/C 2 3 C. Useful Decomposition 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Ramesses knows a lot about problems involving trees (undirected connected graphs without cycles)! 22 23 He created a new useful tree decomposition, but he does not know how to construct it, so he asked you for help! 24 25 The decomposition is the splitting the edges of the tree in some simple paths in such a way that each two paths have at least one common vertex. Each edge of the tree should be in exactly one path. 26 27 Help Remesses, find such a decomposition of the tree or derermine that there is no such decomposition. 28 29 Input 30 31 The first line contains a single integer $n$ ($2 \leq n \leq 10^{5}$) the number of nodes in the tree. 32 33 Each of the next $nā-ā1$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq n$, $a_i \neq b_i$)Ā ā the edges of the tree. It is guaranteed that the given edges form a tree. 34 35 Output 36 37 If there are no decompositions, print the only line containing "No". 38 39 Otherwise in the first line print "Yes", and in the second line print the number of paths in the decomposition $m$. 40 41 Each of the next $m$ lines should contain two integers $u_i$, $v_i$ ($1 \leq u_i, v_i \leq n$, $u_i \neq v_i$) denoting that one of the paths in the decomposition is the simple path between nodes $u_i$ and $v_i$. 42 43 Each pair of paths in the decomposition should have at least one common vertex, and each edge of the tree should be presented in exactly one path. You can print the paths and the ends of each path in arbitrary order. 44 45 If there are multiple decompositions, print any. 46 47 Examples 48 49 Input 50 51 ``` 52 4 53 1 2 54 2 3 55 3 4 56 ``` 57 58 Output 59 60 ``` 61 Yes 62 1 63 1 4 64 ``` 65 66 Input 67 68 ``` 69 6 70 1 2 71 2 3 72 3 4 73 2 5 74 3 6 75 ``` 76 77 Output 78 79 ``` 80 No 81 ``` 82 83 Input 84 85 ``` 86 5 87 1 2 88 1 3 89 1 4 90 1 5 91 ``` 92 93 Output 94 95 ``` 96 Yes 97 4 98 1 2 99 1 3 100 1 4 101 1 5 102 ``` 103 104 Note 105 106 The tree from the first example is shown on the picture below:  The number next to each edge corresponds to the path number in the decomposition. It is easy to see that this decomposition suits the required conditions. 107 108 The tree from the second example is shown on the picture below:  We can show that there are no valid decompositions of this tree. 109 110 The tree from the third example is shown on the picture below:  The number next to each edge corresponds to the path number in the decomposition. It is easy to see that this decomposition suits the required conditions.