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.


-----Input-----

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.


-----Output-----

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.


-----Examples-----
Input
4
1 2
2 3
3 4

Output
Yes
1
1 4

Input
6
1 2
2 3
3 4
2 5
3 6

Output
No

Input
5
1 2
1 3
1 4
1 5

Output
Yes
4
1 2
1 3
1 4
1 5



-----Note-----

The tree from the first example is shown on the picture below: [Image] 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: [Image] We can show that there are no valid decompositions of this tree.

The tree from the third example is shown on the picture below: [Image] 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.

Difficulty: interview

Starter code:

Solutions:
# python3


def readline(): return tuple(map(int, input().split()))


def readlines(count): return (readline() for __ in range(count))


def main():
    n, = readline()
    degree = [0] * n
    root = None
    for (a, b) in readlines(n - 1):
        degree[a - 1] += 1
        degree[b - 1] += 1

    for (x, d) in enumerate(degree, start=1):
        if d > 2:
            if root is None:
                root = x
            else:
                print("No")
                return

    if root is None:
        root = 1

    ways = [i for (i, d) in enumerate(degree, start=1) if d == 1 and i != root]
    print("Yes")
    print(len(ways))
    print("\n".join(map(f"{root} {{}}".format, ways)))


main()


#!/usr/bin/env python3

n = int(input().strip())
degs = [0 for _ in range(n)]
for _ in range(n - 1):
	[u, v] = list(map(int, input().strip().split()))
	degs[u - 1] += 1
	degs[v - 1] += 1

ni = [None, [], [], []]
for v, d in enumerate(degs):
	dd = min(d, 3)
	ni[dd].append(v)


if len(ni[3]) > 1:
	print ('No')
elif len(ni[3]) == 1:
	print ('Yes')
	print(len(ni[1]))
	u = ni[3][0]
	for v in ni[1]:
		print(u + 1, v + 1)
else:  # ni[3] == []
	print ('Yes')
	print(1)
	print(ni[1][0] + 1, ni[1][1] + 1)



from copy import deepcopy
import itertools
from bisect import bisect_left

def read():
    return int(input())


def readmap():
    return map(int, input().split())


def readlist():
    return list(map(int, input().split()))


N = read()
degree = [0] * N
for _ in range(N-1):
    a, b = readmap()
    degree[a-1] += 1
    degree[b-1] += 1

more_than_three = 0
for d in degree:
    if d >= 3:
        more_than_three += 1

if more_than_three >= 2:
    print("No")
    quit()

ans = []
if more_than_three == 1:
    for v in range(N):
        if degree[v] >= 3:
            center = v
    for v in range(N):
        if degree[v] == 1:
            ans.append((center+1, v+1))
    print("Yes")
    print(len(ans))
    for tup in ans:
        print(tup[0], tup[1])
    quit()

leaf = []
for v in range(N):
    if degree[v] == 1:
        leaf.append(v)
print("Yes")
print(1)
print(leaf[0]+1, leaf[1]+1)

n = int(input())
orders = [0 for i in range(n)]
for i in range(n-1):
    a, b = [int(j) -1 for j in input().split()]
    orders[a] += 1
    orders[b] += 1

roots = []
leafs = []

for i, x in enumerate(orders):
    if x > 2:
        roots.append(i)
    elif x == 2:
        pass
    elif x == 1:
        leafs.append(i)
    else:
        raise Exception('woww')


if len(roots) > 1:
    print('No')
elif len(roots) == 0:
    print('Yes')
    print('1')
    print(' '.join([str(l+1) for l in leafs]))
elif len(roots) == 1:
    print('Yes')
    print(str(len(leafs)))
    root = str(roots[0] + 1)
    for l in leafs:
        print(root, str(l+1))
else:
    raise Exception('woww')


def main():
    n = int(input())
    deg = [0] * n
    for i in range(n - 1):
        u, v = map(int, input().split())
        deg[u - 1] += 1
        deg[v - 1] += 1
    count = 0
    ans = 0
    e = []
    for i in range(n):
        if (deg[i] > 2):
            count += 1
            ans = i
        if (deg[i] == 1):
            e.append(i)
    if (count > 1):
        print("No")
    else:
        print("Yes")
        print(deg[ans])
        for el in e:
            if (el != ans):
                print(ans + 1, el + 1)
main()

n=int(input())
g=[0 for i in range(n)]
for i in range(n-1):
    a,b=map(int,input().split())
    g[a-1]+=1
    g[b-1]+=1
q=0
k=0
kind=0
m=0
for i in range(n):
    if g[i]==1:
        m+=1
    if g[i]>2:
        if g[i]>k:
            kind=i
            k=g[i]
        q+=1
if q>1:
    print('No')
else:
    print('Yes')
    if k>2:
        print(m)
        for i in range(n):
            if g[i]==1:
                print(kind+1,i+1)
    else:
        print(1)
        for i in range(n):
            if g[i]==1:
                print(i+1,end=' ')


n = int(input())
graph = [ [] for i in range(n+1)]
center = None
should_continue = True
ends = set()
for i in range(n-1):
    parameters = [int(s) for s in input().split(" ")]
    from_ = parameters[0]
    to = parameters[1]
    # print(graph)
    # print(from_)
    # if not graph[from_]:
    #     graph[from_] = []
    # if not graph[to]:
    #     graph[to] = []
    graph[from_].append(to)
    graph[to].append(from_)
    if len(graph[from_]) > 2:
        if center and center != from_:
            should_continue = False
            break
        center = from_

    if len(graph[to]) > 2:
        if center and center != to:
            should_continue = False
            break
        center = to

    if len(graph[from_]) == 1:
        ends.add(from_)
    else:
        ends.discard(from_)

    if len(graph[to]) == 1:
        ends.add(to)
    else:
        ends.discard(to)

if not should_continue:
    print("No")
else:
    print("Yes")

    if center == None:
        print("1")
        print(str(ends.pop()) + " " + str(ends.pop()))
    else:
        print(len(graph[center]))
        for i in ends:
            print(str(center) + " " + str(i))


n = int(input())
dic = {}
for i in range(n):
    dic.update({i+1:0})
for i in range(n-1):
    s = input().split()
    dic[int(s[0])]+=1
    dic[int(s[1])]+=1

cl = []
count1=0
count2=0
count3=0
for x in dic.keys():
    if dic[x]==1:
        count1+=1
        cl.append(x)
    elif dic[x]==2:
        count2+=1
    else:
        count3+=1
        p = x

if count3==0:
    print('Yes')
    print(1)
    print('{} {}'.format(cl[0], cl[1]))
elif count3==1:
    print('Yes')
    print(count1)
    for i in range(1, n+1):
        if i!=p and dic[i]==1:
            print('{} {}'.format(p, i))
else:
    print('No')

import sys
import filecmp
import math
FILE_IO = False


CURR_PATH = []
list_of_connections = [[] for _ in range(100001)]

def dfs(vertex):
    visited, stack = set(), [vertex]

    while stack:
        vertex = stack.pop()
        if vertex not in visited:
            visited.add(vertex)
            stack.extend(set(list_of_connections[vertex]) - visited)
            if (len(set(list_of_connections[vertex]) - visited) == 0):
                CURR_PATH.append(vertex)




if FILE_IO:
    input_stream = open('input_test.txt')
    sys.stdout = open('current_output2.txt', 'w')
else:
    input_stream = sys.stdin

n = int(input_stream.readline())



for _ in range(n-1):
    input_list = input_stream.readline().split()
    start , end = int(input_list[0]),int(input_list[1])
    list_of_connections[start].append(end)
    list_of_connections[end].append(start)

max_edges = None
max_less_then_3 = None
no = False
for ind, edges in enumerate(list_of_connections):
    current_len = len(edges)
    if current_len > 2:
        if max_less_then_3 is not None:
            print('No')
            return
        else:
            max_less_then_3 = ind

if max_less_then_3 is None:
    max_less_then_3 = 1

print('Yes')
root = max_less_then_3

paths = []

visited = [False] * 100001
dfs(root) 

print(len(CURR_PATH))
for node in CURR_PATH:
    print(str(root) + " "+ str(node))
            


    

# if FILE_IO:
    # assert filecmp.cmp('current_output.txt','expected_output.txt',shallow=False) == True 


n = int(input())
flag = True
f = [0] * (n - 1)
t = [0] * (n - 1)
co = [0] * n
for i in range(n - 1):
    f[i], t[i] = map(int, input().split())
    co[f[i] - 1] += 1
    co[t[i] - 1] += 1
mid = 0
en = []
for i in range(n):
    if co[i] == 1:
        en.append(i + 1)
    elif co[i] > 2:
        if mid == 0:
            mid = i + 1
        else:
            print("No")
            flag = False
            break
if flag:
    print("Yes")
    if mid != 0:
        print(len(en))
        for i in range(len(en)):
            if (en[i] != mid):
                print(mid, en[i])
    else:
        print(1)
        print(en[0], en[1])

n = int(input())
flag = True
f = [0] * (n - 1)
t = [0] * (n - 1)
co = [0] * n
for i in range(n - 1):
    f[i], t[i] = map(int, input().split())
    co[f[i] - 1] += 1
    co[t[i] - 1] += 1
mid = 0
en = []
for i in range(n):
    if co[i] == 1:
        en.append(i + 1)
    elif co[i] > 2:
        if mid == 0:
            mid = i + 1
        else:
            print("No")
            flag = False
            break
if flag:
    print("Yes")
    if mid != 0:
        print(len(en))
        for i in range(len(en)):
            print(mid, en[i])
    else:
        print(1)
        print(en[0], en[1])

n = int(input())
d = [0]*100010
l = []
le = []
for _ in range(n-1):
    u,v = map(int,input().split())
    d[u]+=1
    d[v]+=1
    l.append(u)
    l.append(v)
r = 0
for i in range(1,max(l)+1):
    if(d[i]==1):
        le.append(i)
    if(d[i]>2):
        if(not r):
            r = i
        else:
            print('No')
            return
print('Yes')
if(not r):
    print(1)
    print(min(le),max(le))
else:
    x = len(le)
    print(x)
    le.sort()
    for i in range(1,x+1):
        print(r,le[i-1])

#Zad
from collections import Counter
n=int(input())
tree=[]
for i in range(n-1):
    tree.extend(list(map(int,input().split())))
ans=Counter(tree)
if ans.most_common(2)[1][1]>2: print('No')
else:
    print('Yes')
    if n==2:
        print(1)
        print('1 2')
    else:
        if ans.most_common(1)[0][1]==2:
            print(1)
            print(ans.most_common()[-2][0], ans.most_common()[-1][0])
        else:
            center=ans.most_common(1)[0][0]
            kraya=[n[0] for n in ans.most_common() if n[1]==1]
            print(len(kraya))
            for i in kraya:
                print(center, i)

n = int(input())
flag = True
f = [0] * (n - 1)
t = [0] * (n - 1)
co = [0] * n
for i in range(n - 1):
    f[i], t[i] = map(int, input().split())
    co[f[i] - 1] += 1
    co[t[i] - 1] += 1
mid = 0
en = []
for i in range(n):
    if co[i] == 1:
        en.append(i + 1)
    elif co[i] > 2:
        if mid == 0:
            mid = i + 1
        else:
            print("No")
            flag = False
            break
if flag:
    print("Yes")
    if mid != 0:
        print(len(en))
        for i in range(len(en)):
            if (en[i] != mid):
                print(mid, en[i])
    else:
        print(1)
        print(en[0], en[1])

n = (int(input()))
edges = [0 for i in range(n+1)]
for _ in range(n-1):
	x,y = map(int,input().split())
	edges[x] += 1
	edges[y] += 1

leaves = []
node = None
flag = True
for i in range(1,n+1):
	if(edges[i]==1):
		leaves.append(i)
	elif(edges[i]>2):
		if(not node==None):
			flag = False
			break
		else:
			node = i
if(flag):
	print("Yes")
	if(node==None):
		print(1)
		print(leaves[0],end=' ')
		print(leaves[1])
	else:
		print(len(leaves))
		for item in leaves:
			print(item,end=' ')
			print(node)
else:
	print("No")

d = {}
n = int(input())
for _ in range(n-1):
    a, b = map(int, input().split())
    d[a] = d.get(a, 0) + 1
    d[b] = d.get(b, 0) + 1
zz = list(filter(lambda x: d[x] > 2, d))    
z = len(zz)
y = list(filter(lambda x: d[x] == 1, d))
if z > 1:
    print("No")
elif z == 0:    
    print("Yes")
    print(1)
    print(*y)
else:    
    print("Yes")
    print(len(y))
    for v in y:
        print(zz[0], v)