Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.

You are given a tree with $n$ nodes. In the beginning, $0$ is written on all edges. In one operation, you can choose any $2$ distinct leaves $u$, $v$ and any real number $x$ and add $x$ to values written on all edges on the simple path between $u$ and $v$.

For example, on the picture below you can see the result of applying two operations to the graph: adding $2$ on the path from $7$ to $6$, and then adding $-0.5$ on the path from $4$ to $5$. 

 [Image] 

Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?

Leaf is a node of a tree of degree $1$. Simple path is a path that doesn't contain any node twice.


-----Input-----

The first line contains a single integer $n$ ($2 \le n \le 10^5$) — the number of nodes.

Each of the next $n-1$ lines contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$), meaning that there is an edge between nodes $u$ and $v$. It is guaranteed that these edges form a tree.


-----Output-----

If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO". 

Otherwise, output "YES". 

You can print each letter in any case (upper or lower).


-----Examples-----
Input
2
1 2

Output
YES
Input
3
1 2
2 3

Output
NO
Input
5
1 2
1 3
1 4
2 5

Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6

Output
YES


-----Note-----

In the first example, we can add any real $x$ to the value written on the only edge $(1, 2)$.

 [Image] 

In the second example, one of configurations that we can't reach is $0$ written on $(1, 2)$ and $1$ written on $(2, 3)$.

 $8$ 

Below you can see graphs from examples $3$, $4$:

 [Image] 

 [Image]

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Tree algorithms


Solutions:
import sys
from collections import deque
input = sys.stdin.readline

def ii():
	return int(input())

def mi():
	return map(int, input().rstrip().split())

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

def li():
	return list(input().rstrip())

class Graph:

	def __init__(self, n, Weighted=False, Directed=True, Matrix=False):
		self.sz = n
		self.is_Weighted = Weighted
		self.is_Directed = Directed
		self.is_Matrix = Matrix
		if Matrix:
			if Weighted:
				self.graph = [[0 for _i in range(n)] for _j in range(n)]
			else:
				self.graph = [[0 for _i in range(n)] for _j in range(n)]
		else:
			self.graph = [[] for _i in range(n)]

	def _weighted_add_edge(self, x, y, w):
		if self.is_Matrix:
			self.graph[x][y] = w
		else:
			self.graph[x].append((y, w))

	def _unweighted_add_edge(self, x, y):
		if self.is_Matrix:
			self.graph[x][y] = 1
		else:
			self.graph[x].append(y)

	def add_edge(self, x, y, *w):
		if self.is_Directed:
			if self.is_Weighted:
				self._weighted_add_edge(x, y, w[0])
			else:
				self._unweighted_add_edge(x, y)
		elif self.is_Weighted:
			self._weighted_add_edge(x, y, w[0])
			self._weighted_add_edge(y, x, w[0])
		else:
			self._unweighted_add_edge(x, y)
			self._unweighted_add_edge(y, x)

	def _convert_to_maxrix(self):
		if self.is_Matrix:
			return self
		mat_g = self.__class__(self.sz, Weighted=self.is_Weighted, Directed=self.is_Directed, Matrix=True)
		if self.is_Weighted:
			for i in range(self.sz):
				for j in self.graph[i]:
					mat_g.add_edge(i, j[0], j[1])
		else:
			for i in range(self.sz):
				for j in self.graph[i]:
					mat_g.add_edge(i, j)
		return mat_g

	def __getitem__(self, n):
		return self.graph[n]

	def __str__(self):
		return str([self.graph[i] for i in range(self.sz)])

def main():
	n = ii()
	g = Graph(n, Directed=False)
	for i in range(n - 1):
		(s, t) = mi()
		g.add_edge(s - 1, t - 1)
	for i in range(n):
		if len(g[i]) == 2:
			break
	else:
		print('YES')
		sys.exit()
	print('NO')
main()


n = int(input())
d = {}
(o, t) = (0, 0)
for _ in range(n - 1):
	(s, e) = map(int, input().split())
	if s in d:
		d[s] += 1
	else:
		d[s] = 1
	if e in d:
		d[e] += 1
	else:
		d[e] = 1
for i in d:
	if d[i] == 1:
		o += 1
	elif d[i] == 2:
		t += 1
if t:
	print('NO')
elif o * (o - 1) // 2 >= n - 1:
	print('YES')
else:
	print('NO')


n = int(input())
d = [0] * (10 ** 5 + 5)
for _ in range(n - 1):
	(u, v) = map(int, input().split())
	d[u] += 1
	d[v] += 1
for i in range(1, n + 1):
	if d[i] == 2:
		print('NO')
		exit(0)
print('YES')


from sys import stdin
input = stdin.readline
n = int(input())
a = [[] for i in range(n)]
for i in range(n - 1):
	(c, d) = map(int, input().split())
	a[c - 1].append(d - 1)
	a[d - 1].append(c - 1)
if n == 2:
	print('YES')
else:
	k = 0
	for i in a:
		if len(i) == 2:
			exit(print('NO'))
	print('YES')


n = int(input())
st = [0 for i in range(n)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	st[a - 1] += 1
	st[b - 1] += 1
for i in range(n):
	if st[i] == 2:
		print('NO')
		exit()
print('YES')


n = int(input())
edges = []
for i in range(n - 1):
	edges.append(list(map(int, input().split())))
degree = {}
for (u, v) in edges:
	(degree[u], degree[v]) = (degree.get(u, 0) + 1, degree.get(v, 0) + 1)
Possible = True
for u in degree:
	if degree[u] == 2:
		Possible = False
if Possible:
	print('YES')
else:
	print('NO')


import sys
input = sys.stdin.readline
N = int(input())
X = [[] for i in range(N)]
for i in range(N - 1):
	(x, y) = list(map(int, input().split()))
	X[x - 1].append(y - 1)
	X[y - 1].append(x - 1)
for x in X:
	if len(x) == 2:
		print('NO')
		break
else:
	print('YES')


import collections

def add_on_tree():
	n = int(input())
	edges = []
	for i in range(n - 1):
		edges.append([int(x) for x in input().split()])
	graph = collections.defaultdict(set)
	for (u, v) in edges:
		graph[u].add(v)
		graph[v].add(u)
	ans = 'YES'
	for node in graph:
		if len(graph[node]) == 2:
			ans = 'NO'
			break
	print(ans)
add_on_tree()
exit(0)


n = int(input())
arr = [0] * n
for i in range(0, n - 1):
	l = [int(j) for j in input().split(' ')]
	arr[l[0] - 1] += 1
	arr[l[1] - 1] += 1
print(['YES', 'NO'][2 in arr])


n = int(input())
li = [0 for i in range(n + 1)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	li[a] += 1
	li[b] += 1
print('no' if 2 in li else 'yes')


def solve():
	n = int(input())
	d = {}
	for _ in range(n - 1):
		nums = [int(x) for x in input().split()]
		if nums[0] not in d:
			d[nums[0]] = set()
		d[nums[0]].add(nums[1])
		if nums[1] not in d:
			d[nums[1]] = set()
		d[nums[1]].add(nums[0])
	for x in d.keys():
		if len(d[x]) == 2:
			print('NO')
			return
	print('YES')
solve()


n = int(input())
adj = [0 for i in range(n)]
for i in range(n - 1):
	(u, v) = map(int, input().split())
	(u, v) = (u - 1, v - 1)
	adj[u] += 1
	adj[v] += 1
for i in range(n):
	if adj[i] == 2:
		print('NO')
		exit(0)
print('YES')


n = int(input())
l = [0 for i in range(n)]
for _ in range(n - 1):
	(a, b) = list(map(int, input().split()))
	l[a - 1] += 1
	l[b - 1] += 1
for i in range(n):
	if l[i] == 2:
		print('NO')
		break
else:
	print('YES')


from math import log2
from collections import defaultdict
import sys
input = sys.stdin.readline
inf = 100000000000000000
mod = 998244353
n = int(input())
A = defaultdict(list)
ru = [0] * n
for i in range(n - 1):
	(x, y) = map(int, input().split())
	x -= 1
	y -= 1
	A[x].append(y)
	A[y].append(x)
	ru[x] += 1
	ru[y] += 1
num = 0
flag = 1
for i in range(n):
	if ru[i] == 2:
		flag = 0
if flag == 1:
	print('yes')
else:
	print('no')


n = int(input())
tree_nums = [0 for i in range(n)]
for i in range(n - 1):
	(a1, a2) = map(int, input().split())
	tree_nums[a1 - 1] += 1
	tree_nums[a2 - 1] += 1
if n == 2:
	print('YES')
elif n == 3:
	print('NO')
else:
	ans_is_no = False
	for i in range(n):
		if tree_nums[i] == 2:
			ans_is_no = True
			print('NO')
			break
	if not ans_is_no:
		print('YES')


from collections import defaultdict
n = int(input())
edges = defaultdict(int)
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	edges[a] += 1
	edges[b] += 1
for key in edges:
	if edges[key] == 2:
		print('NO')
		exit()
print('YES')


t = int(input())
lst = [[] for c in range(t + 1)]
for c in range(t - 1):
	(m, n) = map(int, input().split())
	lst[m].append(n)
	lst[n].append(m)
for c in range(len(lst)):
	if len(lst[c]) == 2:
		print('NO')
		exit(0)
print('YES')


I = lambda : map(int, input().split())
n = int(input())
if n == 1 or n == 2:
	print('YES')
	return
if n == 3:
	print('NO')
	return
lii = [[] for i in range(n + 1)]
for i in range(n - 1):
	(x, y) = I()
	lii[x] += [y]
	lii[y] += [x]
pp = 0
for i in range(1, n + 1):
	if len(lii[i]) == 1:
		continue
	elif len(lii[i]) == 2:
		pp = 1
		break
if pp == 1:
	print('NO')
	return
print('YES')
return


n = int(input())
node = []
for i in range(1 + n):
	node.append([])
for q in range(n - 1):
	k = list(map(int, input().split()))
	node[k[0]].append(k[1])
	node[k[1]].append(k[0])
hobe = True
for i in range(n + 1):
	if len(node[i]) == 2:
		hobe = False
if hobe:
	print('YES')
else:
	print('NO')


import sys
num = int(input())
deg = [0] * (num + 1)
for _ in range(1, num):
	(a, b) = [int(i) for i in input().split()]
	deg[a] += 1
	deg[b] += 1
for i in range(1, num + 1):
	if deg[i] == 2:
		print('NO')
		sys.exit()
print('YES')


from collections import defaultdict
n = int(input())
hash = defaultdict(list)
for i in range(n - 1):
	(a, b) = map(int, input().split())
	hash[a].append(b)
	hash[b].append(a)
flag = 1
for i in hash.keys():
	if len(hash[i]) == 2:
		flag = 0
		break
if flag:
	print('YES')
else:
	print('NO')


n = int(input())
List = []
vertecies = [0] * n
for i in range(0, n - 1):
	OP = []
	OP = list(map(int, input().split()))
	List.append(OP)
	vertecies[OP[0] - 1] = vertecies[OP[0] - 1] + 1
	vertecies[OP[1] - 1] = vertecies[OP[1] - 1] + 1
k = 0
for i in range(0, n):
	if vertecies[i] == 2:
		k = k + 1
if k > 0:
	print('NO')
else:
	print('YES')


import sys
n = int(sys.stdin.readline().strip())
d = [0] * n
for i in range(0, n - 1):
	(u, v) = sys.stdin.readline().strip().split()
	(u, v) = (int(u) - 1, int(v) - 1)
	d[u] = d[u] + 1
	d[v] = d[v] + 1
if 2 in d:
	print('NO')
else:
	print('YES')


n = int(input())
nodes = [0] * n
for i in range(0, n - 1):
	for x in map(int, input().split()):
		nodes[x - 1] += 1
if 2 in nodes:
	print('NO')
else:
	print('YES')


def add(x):
	global d, d2
	if x not in d:
		d[x] = 0
	d[x] += 1
	if d[x] == 2:
		d2[x] = a
	if d[x] == 3:
		del d2[x]
import sys
n = int(sys.stdin.readline())
d = {}
d2 = {}
for _ in range(n - 1):
	(a, b) = map(int, sys.stdin.readline().split())
	add(a)
	add(b)
res = 'YES'
for k in d2:
	res = 'NO'
	break
print(res)


n = int(input())
d = [0 for i in range(n + 1)]
for _ in range(n - 1):
	(u, v) = map(int, input().split())
	d[u] += 1
	d[v] += 1
if 2 in set(d):
	print('NO')
else:
	print('YES')


n = int(input())
dic = {}
for i in range(1, n + 1):
	dic[i] = []
for i in range(n - 1):
	(x, y) = map(int, input().split())
	dic[x].append(y)
	dic[y].append(x)
flag = True
for i in dic:
	if len(dic[i]) == 2:
		flag = False
if flag:
	print('YES')
else:
	print('NO')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())
l = [0] * n
f = 0
for i in range(n - 1):
	(a, b) = map(int, input().split())
	l[a - 1] += 1
	l[b - 1] += 1
for j in l:
	if j == 2:
		f = 1
		break
if f == 1:
	print('NO')
else:
	print('YES')


import sys
from math import factorial

class Node:

	def __init__(self, num):
		self.num = num
		self.binds = []
		self.marker = False
		self.val = None

	def add_bind(self, oth):
		self.binds.append(oth)

	def __repr__(self):
		return '<{}: {}{}>'.format(self.num, [i.num for i in self.binds], ', \tval: {}'.format(self.val) if self.val != None else '')

class Graph:

	def __init__(self, size):
		self.size = size
		self.nodes = [None] + [Node(num) for num in range(1, size + 1)]

	def read_input(self):
		for _ in range(1, self.size):
			(i, j) = (int(x) for x in sys.stdin.readline().split())
			self.nodes[i].add_bind(self.nodes[j])
			self.nodes[j].add_bind(self.nodes[i])

	def __repr__(self):
		return '\n'.join((str(node) for node in self.nodes[1:]))

def pairs(n):
	return factorial(n) // (factorial(n - 2) * 2)
N = int(sys.stdin.readline())
g = Graph(N)
g.read_input()
ends = [node for node in g.nodes[1:] if len(node.binds) == 1]
for n in g.nodes[1:]:
	if len(n.binds) == 2:
		print('NO')
		break
else:
	print('YES')


n = int(input())
edges = [0] * n
for i in range(n - 1):
	(a, b) = list(map(int, input().split()))
	edges[a - 1] += 1
	edges[b - 1] += 1
if 2 in edges:
	print('NO')
else:
	print('YES')


n = int(input())
a = []
for i in range(n):
	a.append(0)
for i in range(n - 1):
	(b, c) = input().split()
	a[int(b) - 1] += 1
	a[int(c) - 1] += 1
if2 = False
for i in range(n):
	if a[i] == 2:
		if2 = True
if if2:
	print('NO')
else:
	print('YES')


n = int(input())
ar = [0] * n
for i in range(n - 1):
	(a, b) = map(int, input().split())
	ar[a - 1] += 1
	ar[b - 1] += 1
ans = 'YES'
for i in range(n):
	if ar[i] == 2:
		ans = 'NO'
		break
print(ans)


import sys
input = sys.stdin.readline
from collections import deque
n = int(input())
if n == 2:
	print('YES')
	exit()
if n == 3:
	print('NO')
	exit()
Edges = [[] for _ in range(n)]
for _ in range(n - 1):
	(u, v) = map(lambda x: int(x) - 1, input().split())
	Edges[u].append(v)
	Edges[v].append(u)
for (i, E) in enumerate(Edges):
	if len(E) >= 3:
		root = i
		break
else:
	print('NO')
	exit()
Chi = [[] for _ in range(n)]
Par = [0] * n
q = deque()
q.append(root)
Used = [False] * n
Used[root] = True
while q:
	v = q.popleft()
	for c in Edges[v]:
		if Used[c]:
			continue
		Chi[v].append(c)
		Par[c] = v
		Used[c] = True
		q.append(c)
Leaf = []
for (v, l) in enumerate(Chi):
	if not l:
		Leaf.append(v)
for l in Leaf:
	while True:
		p = Par[l]
		if p == root:
			break
		if len(Chi[p]) == 1:
			print('NO')
			exit()
		l = p
print('YES')


from sys import setcheckinterval, stdin, setrecursionlimit
setcheckinterval(1000)
setrecursionlimit(10 ** 7)

def iin():
	return int(stdin.readline())

def lin():
	return list(map(int, stdin.readline().split()))
n = iin()
adj = [[] for i in range(n)]
for _ in range(n - 1):
	(a, b) = lin()
	adj[a - 1].append(b - 1)
	adj[b - 1].append(a - 1)
for i in adj:
	if len(i) == 2:
		print('NO')
		exit()
print('YES')


from collections import Counter
n = int(input())
x = []
for _ in range(n - 1):
	x.extend(input().split())
print('NO' if 2 in Counter(x).values() else 'YES')


n = int(input())
(a, flag) = ([0] * (n + 1), False)
for i in range(n - 1):
	(u, v) = [int(x) for x in input().split()]
	a[u] += 1
	a[v] += 1
for j in range(1, n + 1):
	if a[j] == 2:
		flag = True
if flag:
	print('NO')
else:
	print('YES')


n = int(input())
l = [0] * (n + 1)
for i in range(n - 1):
	(u, v) = input().split()
	(u, v) = [int(u), int(v)]
	l[u] += 1
	l[v] += 1
l = list(set(l))
if len(l) <= 2:
	print('YES')
elif l[2] == 2:
	print('NO')
else:
	print('YES')


from collections import defaultdict, deque
import sys, heapq, bisect, math, itertools, string, queue, copy, time
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
mod = 10 ** 9 + 7
eps = 10 ** (-7)

def inp():
	return int(input())

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

def inpl_str():
	return list(input().split())
N = inp()
cnt = [0] * N
for _ in range(N - 1):
	(x, y) = inpl()
	cnt[x - 1] += 1
	cnt[y - 1] += 1
for c in cnt:
	if c == 2:
		print('NO')
		break
else:
	print('YES')


n = int(input())
g = [[] for _ in range(n)]
for _ in range(n - 1):
	(u, v) = map(int, input().split())
	g[u - 1].append(v - 1)
	g[v - 1].append(u - 1)
if list(filter(lambda x: len(x) == 2, g)):
	print('NO')
else:
	print('YES')


n = int(input())
adj = [0 for i in range(n)]
for i in range(n - 1):
	iarr = list(map(int, input().split()))
	u = iarr[0] - 1
	v = iarr[1] - 1
	adj[u] += 1
	adj[v] += 1
flag = 0
for i in adj:
	if i == 2:
		flag = 1
		break
if flag == 1:
	print('NO')
else:
	print('YES')


n = int(input())
d = [0] * (n + 1)
for i in range(n - 1):
	(u, v) = list(map(int, input().split()))
	d[u] += 1
	d[v] += 1
ans = 0
for i in range(n):
	if d[i + 1] == 2:
		ans += 1
if ans == 0:
	print('YES')
else:
	print('NO')


import sys
input = sys.stdin.readline
n = int(input())
E = [list(map(int, input().split())) for i in range(n - 1)]
D = [0] * n
for (x, y) in E:
	D[x - 1] += 1
	D[y - 1] += 1
if 2 in D:
	print('NO')
else:
	print('YES')


m = int(input())
l = [0 for _ in range(m + 1)]
for _ in range(m - 1):
	(a, b) = map(int, input().split())
	l[a] += 1
	l[b] += 1
if 2 in l:
	print('NO')
else:
	print('YES')


from sys import stdin
input = stdin.readline
n = int(input())
degree = [0 for i in range(n + 1)]
for _ in range(n - 1):
	(i, j) = [int(i) for i in input().split()]
	degree[i] += 1
	degree[j] += 1
res = False
for i in range(1, n + 1):
	if degree[i] == 2:
		res = True
if res:
	print('NO')
else:
	print('YES')


n = int(input())
l = [[] for i in range(n + 1)]
for y in range(n - 1):
	(u, v) = map(int, input().split())
	l[u].append(v)
	l[v].append(u)
for i in range(1, n + 1):
	if len(l[i]) == 2:
		print('NO')
		break
else:
	print('YES')


N = int(input())
degree = [0] * (N + 1)
data = []
for i in range(N - 1):
	arr = input()
	(u, v) = [int(x) for x in arr.split(' ')]
	data.append([u, v])
	degree[u] += 1
	degree[v] += 1
if N == 2:
	print('YES')
	quit()
if 2 in degree:
	print('NO')
	quit()
print('YES')


n = int(input())
neigh = [[] for _ in range(n)]
for i in range(n - 1):
	(u, v) = map(int, input().split())
	u -= 1
	v -= 1
	neigh[u].append(v)
	neigh[v].append(u)
no = False
for i in range(n):
	if len(neigh[i]) == 2:
		no = True
if no:
	print('NO')
else:
	print('YES')


from collections import defaultdict, deque, Counter
from sys import stdin, stdout
from heapq import heappush, heappop
import math
import io
import os
import math
import bisect

def isPrime(x):
	for i in range(2, x):
		if i * i > x:
			break
		if x % i == 0:
			return False
	return True

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	return list(set(l))

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def digits(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def ceil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

def mapin():
	return map(int, input().split())
n = int(input())
d = [0] * n
for _ in range(n - 1):
	(u, v) = mapin()
	d[u - 1] += 1
	d[v - 1] += 1
if d.count(2) == 0:
	print('YES')
else:
	print('NO')


n = int(input())
degree = [0 for i in range(n)]
for i in range(n - 1):
	a = [int(i) for i in input().split()]
	x = a[0] - 1
	y = a[1] - 1
	degree[x] += 1
	degree[y] += 1
flag = 0
for i in range(n):
	if degree[i] == 2:
		flag = 1
		break
if flag == 0:
	print('YES')
else:
	print('NO')


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
if all((i != 2 for i in deg)):
	print('YES')
else:
	print('NO')


n = int(input())
idict = {}
for i in range(n - 1):
	(f, s) = map(int, input().split())
	if idict.get(f) == None:
		idict[f] = [s]
	else:
		idict[f] += [s]
	if idict.get(s) == None:
		idict[s] = [f]
	else:
		idict[s] += [f]
for i in idict.values():
	if len(i) == 2:
		print('NO')
		break
else:
	print('YES')


from collections import defaultdict
n = int(input())
graph = defaultdict(list)
for _ in range(n - 1):
	(u, v) = input().split()
	graph[u].append(v)
	graph[v].append(u)

def canAdd(graph):
	for u in graph:
		if len(graph[u]) == 2:
			return 'NO'
	return 'YES'
print(canAdd(graph))


import sys
N = int(input())
adj_list = [[] for _ in range(N)]
for _ in range(N - 1):
	(u, v) = list(map(int, input().split()))
	u -= 1
	v -= 1
	adj_list[u].append(v)
	adj_list[v].append(u)
num_leaf = 0
for node in range(N):
	if len(adj_list[node]) == 2:
		print('NO')
		sys.exit(0)
print('YES')


n = int(input())
vertex = [[] for i in range(n + 1)]
for i in range(n - 1):
	(a, b) = map(int, input().strip().split())
	vertex[a].append(b)
	vertex[b].append(a)
can = True
for i in vertex:
	if len(i) == 2:
		can = False
		break
if can:
	print('YES')
else:
	print('NO')


import sys
input = sys.stdin.readline
from collections import deque

class Graph(object):

	def __init__(self, n, d):
		self.n = n
		self.graph = [[] for i in range(n)]
		self.parent = [-1 for i in range(n)]
		self.directed = d

	def addEdge(self, x, y):
		self.graph[x].append(y)
		if not self.directed:
			self.graph[y].append(x)

	def bfs(self, root):
		queue = [root]
		queue = deque(queue)
		vis = [0] * self.n
		while len(queue) != 0:
			element = queue.popleft()
			vis[element] = 1
			count = 0
			for i in self.graph[element]:
				if vis[i] == 0:
					queue.append(i)
					self.parent[i] = element
					vis[i] = 1
					count += 1
			if count == 1 and element != root:
				return False
		return True

	def dfs(self, root, ans):
		stack = [root]
		vis = [0] * self.n
		stack2 = []
		while len(stack) != 0:
			element = stack.pop()
			if vis[element]:
				continue
			vis[element] = 1
			stack2.append(element)
			for i in self.graph[element]:
				if vis[i] == 0:
					self.parent[i] = element
					stack.append(i)
		while len(stack2) != 0:
			element = stack2.pop()
			m = 0
			for i in self.graph[element]:
				if i != self.parent[element]:
					m += ans[i]
			ans[element] = m
		return ans

	def shortestpath(self, source, dest):
		self.bfs(source)
		path = [dest]
		while self.parent[path[-1]] != -1:
			path.append(parent[path[-1]])
		return path[::-1]

	def detect_cycle(self):
		indeg = [0] * self.n
		for i in range(self.n):
			for j in self.graph[i]:
				indeg[j] += 1
		q = deque()
		vis = 0
		for i in range(self.n):
			if indeg[i] == 0:
				q.append(i)
		while len(q) != 0:
			e = q.popleft()
			vis += 1
			for i in self.graph[e]:
				indeg[i] -= 1
				if indeg[i] == 0:
					q.append(i)
		if vis != self.n:
			return True
		return False

	def reroot(self, root, ans):
		stack = [root]
		vis = [0] * n
		while len(stack) != 0:
			e = stack[-1]
			if vis[e]:
				stack.pop()
				continue
			vis[e] = 1
			for i in graph[e]:
				if not vis[e]:
					stack.append(i)
			if self.parent[e] == -1:
				continue
n = int(input())
g = Graph(n, False)
for i in range(n - 1):
	(u, v) = map(int, input().split())
	g.addEdge(u - 1, v - 1)
for i in range(n):
	if len(g.graph[i]) == 1:
		leaf = i
		break
if not g.bfs(leaf):
	print('NO')
else:
	print('YES')


import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
from types import GeneratorType
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import defaultdict as dd, deque as dq
import math, string

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())
MOD = 998244353

def solve():
	N = getInt()
	D = dd(int)
	for n in range(N - 1):
		(A, B) = getInts()
		D[A] += 1
		D[B] += 1
	for (key, val) in D.items():
		if val == 2:
			return 'NO'
	return 'YES'
print(solve())


n = int(input())
t = {}

class node(object):

	def __init__(self):
		self.vois = []

	def insv(self, voi):
		self.vois.append(voi)
for i in range(1, n + 1):
	t[i] = node()
for i in range(n - 1):
	(u, v) = map(int, input().split())
	t[u].insv(v)
	t[v].insv(u)
for i in range(1, n + 1):
	if len(t[i].vois) == 2:
		print('NO')
		break
else:
	print('YES')


n = int(input())
g = {}
p = {}
path = {}
flg = True
for _ in range(n - 1):
	(u, v) = map(int, input().split())
	if u not in g:
		g[u] = []
	g[u].append(v)
	if v not in g:
		g[v] = []
	g[v].append(u)
flg = 'YES'
for x in g:
	if len(g[x]) == 1:
		continue
	if len(g[x]) == 2:
		flg = 'NO'
		break
print(flg)


n = int(input())
d = {}
for e in range(1, n):
	(x, y) = map(int, input().split())
	d[x] = d.get(x, 0) + 1
	d[y] = d.get(y, 0) + 1
if 2 in d.values():
	print('NO')
else:
	print('YES')


n = int(input())
Graph = [0] * n
for i in range(1, n):
	data = [int(s) for s in input().split()]
	Graph[data[0] - 1] += 1
	Graph[data[-1] - 1] += 1
flag = True
for i in Graph:
	if i == 2:
		flag = False
if flag == True:
	print('YES')
else:
	print('NO')


n = int(input())
g = [[] for i in range(n + 1)]
d = [0] * 100001
for i in range(n - 1):
	(u, v) = [int(i) for i in input().split()]
	g[u].append(v)
	g[v].append(u)
	d[u] += 1
	d[v] += 1
for i in d:
	if i == 2:
		print('NO')
		break
else:
	print('YES')


n = int(input())
a = [0] * n
for i in range(n - 1):
	(c, d) = map(int, input().split())
	a[c - 1] += 1
	a[d - 1] += 1
stat = 2 in a
if stat:
	print('NO')
else:
	print('YES')


import sys
from math import log2
import bisect
import heapq
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

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

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
n = int(ri())
arr = [0] * n
for i in range(n - 1):
	(a, b) = Ri()
	a -= 1
	b -= 1
	arr[a] += 1
	arr[b] += 1
flag = True
for i in range(n):
	if arr[i] == 2:
		flag = False
		break
if flag:
	YES()
else:
	NO()


import math
n = int(input())
adj = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	adj[a - 1].append(b - 1)
	adj[b - 1].append(a - 1)
if n == 2:
	print('YES')
elif n == 3:
	print('NO')
else:
	flag = 0
	for i in range(n):
		if len(adj[i]) == 2:
			flag = 1
			break
	print('YES' if flag == 0 else 'NO')


n = input()
n = int(n)
edges = []
for i in range(n - 1):
	edges.append(input())
graph = {}
for i in range(1, n + 1):
	graph[i] = []
for edge in edges:
	x = edge.split(' ')
	x = map(lambda y: int(y), x)
	x = list(x)
	graph[x[0]].append(x[1])
	graph[x[1]].append(x[0])
q = 0
for (key, value) in graph.items():
	if len(value) == 2:
		q = 1
		break
if q == 1:
	print('NO')
if q == 0:
	print('YES')


import math

def main():
	n = int(input())
	g = []
	for _ in range(n):
		g.append([])
	for _ in range(n - 1):
		(u, v) = map(lambda x: int(x) - 1, input().split())
		g[u].append(v)
		g[v].append(u)
	ok = True
	for i in range(n):
		if len(g[i]) == 2:
			ok = False
			break
	print('YES' if ok else 'NO')
main()


from collections import defaultdict
n = int(input())
d = defaultdict(int)
for i in range(n - 1):
	sa = input().split()
	d[sa[0]] += 1
	d[sa[1]] += 1
for v in list(d.values()):
	if v == 2:
		print('NO')
		return
print('YES')


import sys, math
n = int(sys.stdin.readline())
a = [0] * n
for i in range(n - 1):
	(x, y) = map(int, sys.stdin.readline().split())
	a[x - 1] += 1
	a[y - 1] += 1
flag = True
for i in range(n):
	if a[i] == 2:
		print('NO')
		flag = False
		break
if flag:
	print('YES')


n = int(input())
degrees = [0 for _ in range(n)]
for _ in range(n - 1):
	(u, v) = tuple(map(int, input().rstrip().split()))
	degrees[u - 1] += 1
	degrees[v - 1] += 1
res = 'YES'
for d in degrees:
	if d == 2:
		res = 'NO'
print(res)


n = int(input())
deg = [0] * 100001
for i in range(n - 1):
	(u1, v1) = map(int, input().split(' '))
	deg[u1] += 1
	deg[v1] += 1
for i in range(len(deg)):
	if deg[i] == 2:
		print('NO')
		exit()
print('YES')


from collections import defaultdict
g = defaultdict(set)
n = int(input())
for _ in range(n - 1):
	(u, v) = map(int, input().split())
	g[u].add(v)
	g[v].add(u)

def solve(n, g):
	for i in range(1, n + 1):
		if len(g[i]) == 1 or len(g[i]) >= 3:
			continue
		else:
			return False
	return True
solution = solve(n, g)
print('YES' if solution else 'NO')


import sys
input = sys.stdin.readline
n = int(input())
a = [list(map(int, input().split())) for i in range(n - 1)]
tree = [[] for i in range(n)]
for i in range(n - 1):
	tree[a[i][0] - 1].append(a[i][1] - 1)
	tree[a[i][1] - 1].append(a[i][0] - 1)
for i in range(n):
	if len(tree[i]) == 2:
		print('NO')
		exit()
print('YES')


n = int(input())
tr = [0] * (n + 1)
for i in range(n - 1):
	(u, v) = map(int, input().split())
	tr[u] += 1
	tr[v] += 1
for i in range(1, n + 1):
	if tr[i] == 2:
		print('No')
		break
else:
	print('Yes')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)

def toord(c):
	return ord(c) - ord('a')

def lcm(a, b):
	return a * b // lcm(a, b)
mod = 998244353
INF = float('inf')
from math import factorial, sqrt, ceil, floor, gcd
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush

def main():
	n = N()
	ind = [0] * (n + 1)
	for _ in range(n - 1):
		(f, t) = RL()
		ind[f] += 1
		ind[t] += 1
	print('YES' if 2 not in ind else 'NO')
main()


n = int(input())
d = [0] * (n + 1)
for i in range(n - 1):
	(u, v) = map(int, input().split())
	d[u] += 1
	d[v] += 1
an = 1
for i in d:
	if i == 2:
		an = 0
		break
print('YES' if an else 'NO')


def ii():
	return int(input())

def ss():
	return [x for x in input()]

def si():
	return [int(x) for x in input().split()]

def mi():
	return map(int, input().split())
a = ii()
s = [0 for i in range(a)]
for i in range(a - 1):
	(c, d) = [int(x) - 1 for x in input().split()]
	s[c] += 1
	s[d] += 1
if 2 in s:
	print('NO')
else:
	print('YES')


import sys
input = sys.stdin.readline
N = int(input())
graph = [[] for _ in range(N)]
for _ in range(N - 1):
	(a, b) = map(int, input().split())
	graph[a - 1].append(b - 1)
	graph[b - 1].append(a - 1)
ok = True
for i in range(N):
	if len(graph[i]) == 2:
		ok = False
if ok:
	print('YES')
else:
	print('NO')


import sys
input = sys.stdin.readline
n = int(input())
d = [0] * n
for _ in range(n - 1):
	(u, v) = map(int, input().split())
	d[u - 1] += 1
	d[v - 1] += 1
if 2 in d:
	print('NO')
else:
	print('YES')


from collections import defaultdict
from sys import stdin
input = stdin.readline

def getAns(d):
	for i in d.keys():
		if d[i] == 2:
			print('NO')
			return
	print('YES')
d = defaultdict()
n = int(input())
for i in range(n - 1):
	(u, v) = map(int, input().split())
	d[u] = d.get(u, 0) + 1
	d[v] = d.get(v, 0) + 1
getAns(d)


n = int(input())
deg = [0 for i in range(n + 1)]
for i in range(n - 1):
	(a, b) = [int(j) for j in input().split(' ')]
	deg[a] += 1
	deg[b] += 1
ans = 'YES'
for i in range(n + 1):
	if deg[i] == 2:
		ans = 'NO'
		break
print(ans)


import sys, atexit
from io import BytesIO
inp = BytesIO(sys.stdin.buffer.read())
input = lambda : inp.readline().decode('ascii')
buf = BytesIO()
sys.stdout.write = lambda s: buf.write(s.encode('ascii'))
atexit.register(lambda : sys.__stdout__.buffer.write(buf.getvalue()))
n = int(input())
deg = [0 for _ in range(n)]
for _ in range(n - 1):
	(a, b) = [int(x) - 1 for x in input().split()]
	deg[a] += 1
	deg[b] += 1
if any((x == 2 for x in deg)):
	print('NO')
else:
	print('YES')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import deque, defaultdict
n = int(input())
tree = [[] for s in range(n)]
for s in range(n - 1):
	(u, v) = map(int, input().split())
	u -= 1
	v -= 1
	tree[u].append(v)
	tree[v].append(u)
broke = False
for s in range(n):
	if len(tree[s]) == 2:
		broke = True
		break
if broke == False:
	print('YES')
else:
	print('NO')


n = int(input())
l = [0 for a in range(n)]
for _ in range(n - 1):
	(a, b) = map(int, input().split(' '))
	l[a - 1] += 1
	l[b - 1] += 1
for i in range(n):
	if l[i] == 2:
		print('NO')
		return
print('YES')


from collections import Counter
n = int(input())
d = Counter()
for _ in range(n - 1):
	(u, v) = map(int, input().split())
	d[u] += 1
	d[v] += 1
if any((x == 2 for x in d.values())):
	print('NO')
else:
	print('YES')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
import threading
threading.stack_size(10 ** 8)
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def solve():
	n = int(ri())
	g = [[] for i in range(n)]
	for i in range(n - 1):
		(a, b) = Ri()
		a -= 1
		b -= 1
		g[a].append(b)
		g[b].append(a)
	flag = [True]

	def dfs1(cur, par):
		if len(g[cur]) == 2:
			flag[0] = False
			return
		for child in g[cur]:
			if child == par:
				continue
			dfs1(child, cur)
		return
	dfs1(0, -1)
	if flag[0]:
		YES()
	else:
		NO()
threading.Thread(target=solve).start()


import sys
input = lambda : sys.stdin.readline().strip()
n = int(input())
graph = {}
for i in range(1, n + 1):
	graph[i] = []
for i in range(1, n):
	(a, b) = map(int, input().split())
	graph[a].append(b)
	graph[b].append(a)
for i in range(1, n + 1):
	if len(graph[i]) == 2:
		print('NO')
		break
else:
	print('YES')


n = int(input())
dep = []
for i in range(n):
	dep.append(0)
for i in range(n - 1):
	(a, b) = input().split(' ')
	a = int(a)
	b = int(b)
	dep[a - 1] = dep[a - 1] + 1
	dep[b - 1] = dep[b - 1] + 1
good = 1
for i in range(n):
	if dep[i] == 2:
		good = 0
if good == 1:
	print('YES')
else:
	print('NO')


import sys
try:
	(sys.stdin, sys.stdout) = (open('in.txt', 'r'), open('out.txt', 'w'))
except:
	pass
ii1 = lambda : int(sys.stdin.readline().strip())
is1 = lambda : sys.stdin.readline().strip()
iia = lambda : list(map(int, sys.stdin.readline().strip().split()))
isa = lambda : sys.stdin.readline().strip().split()
mod = int(1000000000.0 + 7)
from collections import *
from math import *
node = ii1()
tree = {}
for i in range(node - 1):
	(u, v) = iia()
	tree[u] = tree.get(u, 0) + 1
	tree[v] = tree.get(v, 0) + 1
flag = False
for (key, val) in tree.items():
	if val == 2:
		flag = True
		break
if flag:
	print('NO')
else:
	print('YES')


import sys
try:
	import networkx as nx
	from random import randint, random
	import matplotlib.pyplot as plt
	from matplotlib import MatplotlibDeprecationWarning
	from warnings import filterwarnings
	filterwarnings('ignore', category=MatplotlibDeprecationWarning)
	import os
except:
	pass

class Node:

	def __init__(self, num):
		self.num = num
		self.neighbs = []
		self.nedges = []
		self.marker = False
		self.val = None

	def add_neigh(self, oth, edge_weight=None):
		self.neighbs.append(oth)
		oth.neighbs.append(self)
		e = Edge(self, oth, edge_weight)
		self.nedges.append(e)
		oth.nedges.append(e)
		return e

	def is_leaf(self):
		return len(self.neighbs) == 1

	def __repr__(self):
		return '({}: {}{}{})'.format(self.num, [i.num for i in self.neighbs], ', val: {}'.format(self.val) if self.val != None else '', ', Marked' if self.marker else '')

class Edge:

	def __init__(self, n0, n1, weight=None):
		self.ends = [n0, n1]
		self.weight = weight
		self.marker = False

	def __repr__(self):
		return '<{} -- {}{}{}>'.format(self.ends[0].num, self.ends[1].num, ', weight: {}'.format(self.weight) if self.weight != None else '', ', Marked' if self.marker else '')

class Graph:

	def __init__(self, N=None, E=None):
		self.N = N
		self.E = E
		self.nodes = []
		self.edges = []
		self.OFFSET = 1

	def read_edges(self, file=sys.stdin):
		nodes = {}
		for _ in range(self.E):
			vals_arr = file.readline().split()
			(i, j) = tuple((int(x) for x in vals_arr[:2]))
			if len(vals_arr) > 2:
				try:
					val = int(vals_arr[2])
				except ValueError:
					val = float(vals_arr[2])
			else:
				val = None
			if i not in nodes:
				nodes[i] = Node(i)
			if j not in nodes:
				nodes[j] = Node(j)
			e = nodes[i].add_neigh(nodes[j], val)
			self.edges.append(e)
		self.nodes = [nodes[i] for i in range(self.OFFSET, len(nodes) + self.OFFSET)]
		if self.N == None:
			self.N = len(self.nodes)
		else:
			assert self.N == len(self.nodes), 'N = {}, len = {}'.format(self.N, len(self.nodes))

	def read_nodes(self, file=sys.stdin):
		self.nodes = [Node(i) for i in range(self.OFFSET, self.N + self.OFFSET)]
		for i in range(self.OFFSET, self.N + self.OFFSET):
			ni = self.nodes[i - self.OFFSET]
			ints = tuple((int(x) for x in file.readline().split()))
			for j in ints:
				nj = self.nodes[j - self.OFFSET]
				if nj not in ni.neighbs:
					e = ni.add_neigh(nj)
					self.edges.append(e)
		if self.E == None:
			self.E = len(self.edges)
		else:
			assert self.E == len(self.edges), 'E = {}, len = {}'.format(self.E, len(self.edges))

	def write_edges(self, file=sys.stdout):
		for e in self.edges:
			print('{} {}{}'.format(e.ends[0].num, e.ends[1].num, ' {}'.format(e.weight) if e.weight != None else ''), file=file)

	def write_nodes(self, file=sys.stdout):
		sorted_nodes = sorted(self.nodes, key=lambda x: x.num)
		for n in sorted_nodes:
			print(' '.join((str(x.num) for x in n.neighbs)), file=file)

	def gen_rand_tree(self, weighted=False, is_weights_int=True):
		if self.E != None:
			print('Number of edges will be changed')
		tries = max(1000, self.N ** 2)
		g = nx.random_powerlaw_tree(self.N, tries=tries)
		relabel_map = {i: i + 1 for i in range(self.N)}
		nx.relabel_nodes(g, relabel_map)
		self.E = len(g.edges())
		self.nodes = [Node(i) for i in range(self.OFFSET, self.N + self.OFFSET)]
		for nx_e in g.edges():
			(i, j) = (nx_e[0], nx_e[1])
			(ni, nj) = (self.nodes[i], self.nodes[j])
			e = ni.add_neigh(nj)
			self.edges.append(e)
		if weighted:
			for e in self.edges:
				w = randint(0, 10) if is_weights_int else round(random() * 10, 1)
				e.weight = w

	def check(self):
		assert len(self.nodes) == self.N
		assert len(self.edges) == self.E
		self.drop_edge_markers()
		for node in self.nodes:
			assert len(node.neighbs) == len(node.nedges), 'Incorrect node {}'.format(node) + '\nlen(.neighbs): {}'.format(len(node.neighbs)) + '\nlen(.nedges): {}'.format(len(node.nedges))
			for (neith, nedge) in zip(node.neighbs, node.nedges):
				assert len(nedge.ends) == 2, 'Too many ends of edge {}:\n{}'.format(nedge, nedge.ends)
				assert node in nedge.ends, 'Edge {}'.format(nedge) + ' exists in node {}'.format(node) + ' neighbors list, but not vice versa'
				oth_end = nedge.ends[0] if node is nedge.ends[1] else nedge.ends[1]
				assert neith is oth_end, 'Node {}'.format(neith) + ' should be the end of edge {}'.format(nedge) + ' instead of {}'.format(oth_end)
				nedge.marker = True
		for e in self.edges:
			assert e.marker, 'Isolated edge {}'.format(e)
			e.marker = False

	def drop_node_markers(self):
		for node in self.nodes:
			node.marker = False

	def init_node_vals(self, val):
		for node in self.nodes:
			node.val = val

	def drop_edge_markers(self):
		for edge in self.edges:
			edge.marker = False

	def init_edge_weights(self, val):
		for edge in self.edges:
			edge.val = val

	def draw(self, weighted=False, dot_output=False, interactive=True):
		g = nx.Graph()
		for n in self.nodes:
			g.add_node(n.num, color='red' if n.marker else 'black')
		for e in self.edges:
			if weighted:
				g.add_edge(e.ends[0].num, e.ends[1].num, label=e.weight, color='red' if e.marker else 'black')
			else:
				g.add_edge(e.ends[0].num, e.ends[1].num, color='red' if e.marker else 'black')
		pos = nx.spring_layout(g)
		ecol_map = ['red' if e.marker else 'black' for e in self.edges]
		ncol_map = ['red' if n.marker else 'gray' for n in self.nodes]
		nx.draw_networkx(g, pos=pos, node_color=ncol_map, edge_color=ecol_map, with_labels=True)
		if weighted:
			elab_map = nx.get_edge_attributes(g, 'label')
			nx.draw_networkx_edge_labels(g, pos=pos, edge_labels=elab_map)
		if dot_output:
			nx.drawing.nx_pydot.write_dot(g, 'fig.dot')
		plt.axis('off')
		plt.savefig('fig.png')
		if interactive:
			plt.show()
		plt.clf()

	def __repr__(self):
		return '\n'.join((str(node) for node in self.nodes))

	def __getitem__(self, i):
		return self.nodes[i - self.OFFSET]

def solve_ABC(A, B, C, edge, NEXT, actions, w=0):
	actions.append((A.num, B.num, w // 2))
	actions.append((A.num, C.num, w // 2))
	actions.append((B.num, C.num, -w // 2))

def traversal(CUR, edge, ROOT, graph_leafs, actions, prev_edge=None):
	NEXT = next((n for n in edge.ends if n is not CUR))
	if NEXT.is_leaf():
		for (OTH_LEAF, nedge) in zip(CUR.neighbs, CUR.nedges):
			if nedge is not edge and nedge is not prev_edge:
				break
		while not OTH_LEAF.is_leaf():
			nedge = next((e for e in OTH_LEAF.nedges if e is not nedge))
			OTH_LEAF = next((n for n in nedge.ends if n is not OTH_LEAF))
		solve_ABC(NEXT, ROOT, OTH_LEAF, edge, CUR, actions, edge.weight)
		edge.weight = 0
		return (NEXT, 0)
	else:
		leafs = []
		cumul_w = 0
		for (neigh, nedge) in zip(NEXT.neighbs, NEXT.nedges):
			if neigh is CUR:
				continue
			(leaf, w) = traversal(NEXT, nedge, ROOT, graph_leafs, actions, edge)
			leafs.append(leaf)
			cumul_w += w
		solve_ABC(ROOT, leafs[0], leafs[1], edge, NEXT, actions, edge.weight - cumul_w)
		return (leafs[0], edge.weight)

def solve(g):
	from time import sleep
	if len(g.edges) == 1:
		e = g.edges[0]
		print('1\n1 2 {}'.format(e.weight))
		return
	graph_leafs = [n for n in g.nodes if len(n.neighbs) == 1]
	R = graph_leafs[0]
	edge = R.nedges[0]
	actions = []
	traversal(R, edge, R, graph_leafs, actions)
	print(len(actions))
	for a in actions:
		print(' '.join((str(i) for i in a)))
N = int(sys.stdin.readline())
E = N - 1
g = Graph(N, E)
g.read_edges()
ends = [node for node in g.nodes if len(node.neighbs) == 1]
for n in g.nodes:
	if len(n.neighbs) == 2:
		print('NO')
		break
else:
	print('YES')
	solve(g)


n = int(input())
tree = {}
for i in range(n - 1):
	(a, b) = [int(x) for x in input().split()]
	if a not in tree:
		tree[a] = 1
	else:
		tree[a] += 1
	if b not in tree:
		tree[b] = 1
	else:
		tree[b] += 1
for item in tree:
	if tree[item] == 2:
		print('NO')
		break
else:
	print('YES')


n = int(input())
cnt = [0] * (n + 1)
for i in range(n - 1):
	(a, b) = map(int, input().split())
	cnt[a] += 1
	cnt[b] += 1
f = 0
for i in range(n + 1):
	if cnt[i] == 2:
		print('no')
		f = 1
		break
if f == 0:
	print('yes')


import getpass
import sys
if getpass.getuser() != 'frohenk':
	filename = 'half'
else:
	sys.stdin = open('input.txt')
import math
import string
import re
import random
from decimal import Decimal, getcontext
from collections import deque
mod = 10 ** 9 + 7

def ria():
	return [int(i) for i in input().split()]
n = ria()[0]
ar = [0] * n
for i in range(n - 1):
	(a, b) = ria()
	a -= 1
	b -= 1
	ar[a] += 1
	ar[b] += 1
print('NO' if ar.count(2) else 'YES')


import sys
import math

def read_line():
	return sys.stdin.readline()[:-1]

def read_int():
	return int(sys.stdin.readline())

def read_int_line():
	return [int(v) for v in sys.stdin.readline().split()]

def read_float_line():
	return [float(v) for v in sys.stdin.readline().split()]
n = read_int()
d = {}
for i in range(n - 1):
	(u, v) = read_int_line()
	if u in d:
		d[u] += 1
	else:
		d[u] = 1
	if v in d:
		d[v] += 1
	else:
		d[v] = 1
f = False
for i in d:
	if d[i] == 2:
		f = True
if f:
	print('NO')
else:
	print('YES')


n = int(input())
gr = [[] for i in range(n + 1)]
deg = [0 for i in range(n + 1)]
for _ in range(n - 1):
	(u, v) = map(int, input().split())
	gr[u].append(v)
	gr[v].append(u)
	deg[u] += 1
	deg[v] += 1
flag = 1
ans = 0
for i in range(1, n + 1):
	if deg[i] != 1 and deg[i] <= 2:
		ans = 1
if ans == 1:
	print('NO')
else:
	print('YES')


n = int(input())
vertex = [[] for i in range(n + 1)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	vertex[a].append(b)
	vertex[b].append(a)
for i in vertex:
	if len(i) == 2:
		print('NO')
		break
else:
	print('YES')


from queue import PriorityQueue
from queue import Queue
import math
from collections import defaultdict
import sys
import operator as op
from functools import reduce
i = lambda : list(map(int, sys.stdin.readline().split()))
(n,) = i()
arr = [0] * n
for _ in range(n - 1):
	(u, v) = i()
	arr[u - 1] += 1
	arr[v - 1] += 1
fl = 0
for k in arr:
	if k == 2:
		fl = 1
		break
if fl == 0:
	print('YES')
else:
	print('NO')


import sys

class D1AddOnATree:

	def dfs(self, start):
		self.done[start] = 1
		for x in self.adj[start]:
			if self.done[x]:
				continue
			self.par[x] = start
			self.dfs(x)

	def solve(self):
		from collections import defaultdict
		import sys
		sys.setrecursionlimit(10 ** 5 + 5)
		n = int(input())
		self.adj = defaultdict(list)
		self.par = defaultdict(int)
		self.done = [0] * (n + 1)
		for i in range(n - 1):
			(u, v) = [int(_) for _ in input().split()]
			self.adj[u].append(v)
			self.adj[v].append(u)
			self.done[u] += 1
			self.done[v] += 1
		for i in range(1, n + 1):
			if self.done[i] == 2:
				print('NO')
				break
		else:
			print('YES')
		return
		v = max((len(self.adj[p]) for p in self.adj))
		start = -1
		for p in self.adj:
			if len(self.adj[p]) == v:
				start = p
				break
		assert start != -1
		self.dfs(start)
		cnt = [0] * (n + 1)
		for k in self.adj:
			if self.par[k] == 0:
				continue
			if len(self.adj[k]) == 1:
				cnt[self.par[k]] += 1
		ans = 0
		for x in cnt:
			if x == 1:
				ans += 1
		if ans == 0:
			print('YES')
		else:
			print('NO')
solver = D1AddOnATree()
input = sys.stdin.readline
solver.solve()


n = int(input())
g = [[] for i in range(n + 1)]
for i in range(n - 1):
	(u, v) = map(int, input().split())
	g[v].append(u)
	g[u].append(v)
flag = 1
for i in range(1, n + 1):
	if len(g[i]) == 2:
		flag = 0
		print('NO')
		break
if flag:
	print('YES')


import sys
input = sys.stdin.readline

def getN():
	return int(input())

def getList():
	return list(map(int, input().split()))
from sys import exit
n = getN()
vertex = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = getList()
	vertex[a - 1].append(b - 1)
	vertex[b - 1].append(a - 1)
for v in vertex:
	if len(v) == 2:
		print('NO')
		exit()
print('YES')


n = int(input())
adj = [[] for i in range(n)]
for _ in range(n - 1):
	(u, v) = map(int, input().split())
	adj[u - 1].append(v)
	adj[v - 1].append(u)
for v in adj:
	if len(v) == 2:
		print('NO')
		break
else:
	print('YES')


n = int(input())
tree = [[] for i in range(n)]
for i in range(n - 1):
	(u, v) = map(int, input().split())
	tree[u - 1].append(v - 1)
	tree[v - 1].append(u - 1)
if n == 2:
	print('YES')
	exit()
for i in range(n):
	if len(tree[i]) == 2:
		print('NO')
		break
else:
	print('YES')


n = int(input())
nodes = [[] for _ in range(n)]
edges = []
for _ in range(n - 1):
	(u, v) = sorted(map(int, input().split(' ')))
	u -= 1
	v -= 1
	nodes[u].append(v)
	nodes[v].append(u)
	edge = [u, v]
	edges.append(edge)
leaf_nodes = [node for node in nodes if len(node) == 1]
num_ads = [len(node) for node in nodes]
if 2 in num_ads:
	out = 'NO'
else:
	out = 'YES'
print(out)


n = int(input())
degree = [0 for i in range(n + 1)]
for i in range(n - 1):
	(u, v) = map(int, input().split())
	degree[u] += 1
	degree[v] += 1
f = 0
for i in range(len(degree)):
	if degree[i] == 2:
		f = 1
		break
if f == 0:
	print('YES')
else:
	print('NO')


n = int(input())
a = []
for i in range(0, n + 9):
	a.append(0)
for i in range(1, n):
	(u, v) = map(int, input().split())
	a[u] = a[u] + 1
	a[v] = a[v] + 1
flag = 1
for i in range(1, n + 1):
	if a[i] == 2:
		flag = 0
if flag == 0:
	print('NO')
else:
	print('YES')


import sys
n = int(input())
g = [[] for _ in range(n)]
for i in range(n - 1):
	(u, v) = map(int, input().split())
	u -= 1
	v -= 1
	g[u].append(v)
	g[v].append(u)
Ok = True
for i in range(n):
	if len(g[i]) == 2:
		Ok = False
print('YES' if Ok else 'NO')


import sys
N = int(input())
Edge = [[] for _ in range(N)]
Dim = [0] * N
for _ in range(N - 1):
	(a, b) = map(int, sys.stdin.readline().split())
	a -= 1
	b -= 1
	Edge[a].append(b)
	Edge[b].append(a)
	Dim[a] += 1
	Dim[b] += 1
Leaf = [d for d in Dim if d == 1]
if [d for d in Dim if d == 2]:
	print('NO')
elif len(Leaf) * (len(Leaf) - 1) // 2 < N - 1:
	print('NO')
else:
	print('YES')


from collections import defaultdict
n = int(input())
tr = defaultdict(int)
for i in range(n - 1):
	(u, v) = map(int, input().split())
	tr[u] += 1
	tr[v] += 1
for i in tr:
	if tr[i] == 2:
		print('NO')
		break
else:
	print('YES')


from collections import Counter
from sys import stdin
n = int(input())
lst = []
for _ in range(n - 1):
	(a, b) = map(int, stdin.readline().split())
	lst.append(a)
	lst.append(b)
fg = 0
for (_, i) in Counter(lst).items():
	if i == 2:
		fg = 1
		break
if fg == 1:
	print('NO')
else:
	print('YES')


from collections import Counter
n = int(input())
counter = Counter()
for i in range(n - 1):
	(a, b) = map(int, input().split())
	counter.update([a, b])
if 2 in counter.values():
	print('NO')
else:
	print('YES')


n = int(input())
dic = {}
for x in range(n - 1):
	(a, b) = list(map(int, input().split()))
	if a in dic:
		dic[a] += 1
	else:
		dic[a] = 1
	if b in dic:
		dic[b] += 1
	else:
		dic[b] = 1
flag = 5
for it in dic:
	if dic[it] == 2:
		flag = 6
		break
if flag == 6:
	print('NO')
else:
	print('YES')


n = int(input())
arr = [0] * n
for i in range(0, n - 1):
	(x, y) = map(int, input().split())
	arr[x - 1] += 1
	arr[y - 1] += 1
for i in range(0, n):
	if arr[i] == 2:
		print('NO')
		exit()
print('YES')


a = int(input())
A = [0] * a
k = 0
for i in range(a - 1):
	(q, w) = map(int, input().split())
	A[q - 1] += 1
	if A[q - 1] == 2:
		k += 1
	elif A[q - 1] == 3:
		k -= 1
	A[w - 1] += 1
	if A[w - 1] == 2:
		k += 1
	elif A[w - 1] == 3:
		k -= 1
if k == 0:
	print('YES')
else:
	print('NO')


from sys import stdin
input = stdin.readline
n = int(input())
g = [[] for _ in range(n)]
for _ in range(n - 1):
	(u, v) = map(int, input().split())
	g[u - 1].append(v - 1)
	g[v - 1].append(u - 1)
flag = True
for i in range(n):
	if len(g[i]) == 2:
		flag = False
if flag:
	print('YES')
else:
	print('NO')


from bisect import bisect_left as bl, bisect_right as br, insort
import sys
import heapq
from math import *
from collections import defaultdict as dd, deque

def data():
	return sys.stdin.readline().strip()

def mdata():
	return map(int, data().split())
n = int(data())
t = [{} for i in range(n)]
for i in range(n - 1):
	(u, v) = mdata()
	t[u - 1][v - 1] = 0
	t[v - 1][u - 1] = 0
for i in range(n):
	if len(t[i]) == 2:
		print('NO')
		exit()
print('YES')


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
import threading
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())
edge = [0] * n
for i in range(n - 1):
	(x, y) = map(int, input().split())
	x -= 1
	y -= 1
	edge[x] += 1
	edge[y] += 1
if 2 in edge:
	print('no')
else:
	print('yes')


n = int(input())
L = []
for i in range(n):
	s = []
	L.append(s)
for i in range(n - 1):
	(a, b) = map(int, input().split())
	L[a - 1].append(b - 1)
	L[b - 1].append(a - 1)
flag = 0
for i in range(0, len(L)):
	if len(L[i]) == 2:
		flag = 1
		break
if flag == 0:
	print('YES')
else:
	print('NO')


n = int(input())
counts = [0] * n
for _ in range(n - 1):
	(u, v) = map(int, input().split())
	u -= 1
	v -= 1
	counts[u] += 1
	counts[v] += 1
all_possible = all((count != 2 for count in counts))
if all_possible:
	print('YES')
else:
	print('NO')


from sys import stdin, stdout, exit
n = int(input())
graph = [[] for i in range(n)]
for i in range(n - 1):
	(u, v) = map(int, stdin.readline().split())
	graph[u - 1].append(v - 1)
	graph[v - 1].append(u - 1)
for i in range(n):
	if len(graph[i]) == 2:
		print('NO')
		exit()
print('YES')


n = int(input())
arr = [0] * n
for i in range(n - 1):
	(a, b) = map(int, input().split())
	arr[a - 1] += 1
	arr[b - 1] += 1
c = 0
for i in range(n):
	c += arr[i] == 2
if c:
	print('NO')
else:
	print('YES')


from types import GeneratorType

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return wrappedfunc

class Graph:

	def __init__(self, V):
		self.V = V
		self.adj = [[] for i in range(V)]

	@bootstrap
	def DFSUtil(self, temp, v, visited, depth, parent):
		visited[v] = True
		temp.append(v)
		for i in self.adj[v]:
			if visited[i] == False:
				depth[i] = depth[v] + 1
				parent[i] = v
				temp = (yield self.DFSUtil(temp, i, visited, depth, parent))
		yield temp

	def addEdge(self, v, w):
		self.adj[v].append(w)
		self.adj[w].append(v)
n = int(input())
G = Graph(n)
for _ in range(n - 1):
	(u, v) = list(map(int, input().split()))
	u -= 1
	v -= 1
	G.addEdge(u, v)
visited = [False] * n
depth = [0] * n
parent = [0] * n
degree = [0] * n
for i in range(n):
	degree[i] = len(G.adj[i])
if 2 in degree:
	print('NO')
else:
	print('YES')


n = int(input())
edges = [0] * (n + 1)
isPossible = True
for i in range(n - 1):
	(a, b) = [int(i) for i in input().split()]
	edges[a] += 1
	edges[b] += 1
for d in edges:
	if d == 2:
		isPossible = False
		break
print('YES' if isPossible else 'NO')