Useful Decomposition
Ramesses knows a lot about problems involving trees (undirected connected graphs without cycles)!He created a new useful tree decomposition, but he does not know how to construct it, so he asked you for help!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.Help Remesses, find such a decomposition of the tree or derermine that there is no such decomposition.
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.The tree from the second example is shown on the picture below:  We can show that there are no valid decompositions of this tree.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.
The first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^{5}$$$) the number of nodes in the tree.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.
If there are no decompositions, print the only line containing "No".Otherwise in the first line print "Yes", and in the second line print the number of paths in the decomposition $$$m$$$. 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$$$. 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.If there are multiple decompositions, print any.
def solve():
    n = int(input())
    d = {}
    arr = []
    for _ in range(n - 1):
        (x, y) = [elem for elem in input().split(' ')]
        if d.get(x):
            d[x] += 1
        else:
            d[x] = 1
        if d.get(y):
            d[y] += 1
        else:
            d[y] = 1
        arr.append(f'{x} {y}')
    m = 0
    m_p = ''
    out = ''
    for (key, value) in d.items():
        if value > m:
            m = value
            m_p = key
    if m > 2:
        cnt = 0
        for (key, value) in d.items():
            if key != m_p and value > 2:
                print('No')
                return
            elif value == 1:
                out += f'{m_p} {key}\n'
                cnt += 1
        print('Yes')
        print(cnt)
        print(out[:-1])
    else:
        elem = []
        for (key, value) in d.items():
            if value == 1:
                elem.append(key)
                if len(elem) == 2:
                    break
        print('Yes')
        print(1)
        print(' '.join(elem))
solve()
n = int(input())
node_count = {}
for i in range(n - 1):
    tmp = input().split()
    for node in tmp:
        if node in node_count:
            node_count[node] += 1
        else:
            node_count[node] = 1
new_data = {}
for node in node_count:
    key = node_count[node]
    if key in new_data:
        new_data[key].append(node)
    else:
        new_data[key] = [node]
count_1 = 0
count_2 = 0
count_high = 0
if 1 in new_data:
    count_1 = len(new_data[1])
if 2 in new_data:
    count_2 = len(new_data[2])
if count_1 != 0 and count_1 in new_data:
    count_high = len(new_data[count_1])
flag = 0
if count_1 + count_2 == len(node_count):
    flag = 1
elif count_1 + count_2 + count_high == len(node_count):
    flag = 2
if flag == 1:
    print('Yes')
    print(1)
    print(new_data[1][0] + ' ' + new_data[1][1])
elif flag == 2:
    print('Yes')
    print(len(new_data[1]))
    for node in new_data[1]:
        print(new_data[count_1][0] + ' ' + node)
else:
    print('No')
def main():
    nNodes = int(input())
    neighborsOf = {node: [] for node in map(str, range(1, nNodes + 1))}
    (threeNeighborNodeExists, threeNeighborNode) = (False, None)
    for i in range(1, nNodes):
        (node1, node2) = input().split()
        (neighborsOf[node1].append(node2), neighborsOf[node2].append(node1))
        if len(neighborsOf[node1]) == 3:
            if threeNeighborNodeExists:
                return 'No'
            (threeNeighborNodeExists, threeNeighborNode) = (True, node1)
        elif len(neighborsOf[node2]) == 3:
            if threeNeighborNodeExists:
                return 'No'
            (threeNeighborNodeExists, threeNeighborNode) = (True, node2)
    if threeNeighborNodeExists:
        return tnnDecompositionFrom(neighborsOf, threeNeighborNode + ' ')
    return decompositionFrom(neighborsOf)

def tnnDecompositionFrom(tree, threeNeighborNode):
    paths = tuple((threeNeighborNode + node for node in tree if len(tree[node]) == 1))
    return f'Yes\n{len(paths)}\n' + '\n'.join(paths)

def decompositionFrom(tree):
    return 'Yes\n1\n' + ' '.join((node for node in tree if len(tree[node]) == 1))
if __name__ == '__main__':
    print(main())