문제
링크
코드
from collections import deque
n = int(input())
graph = [[] for _ in range(n+1)]
visit = [0] * (n+1)
distance = [0] * (n+1)
for _ in range(n-1):
a, b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
def bfs(v, graph) :
queue = deque()
queue.append(v)
visit[v] = 1
while queue :
v = queue.popleft()
for g in graph[v] :
if visit[g] != 1 :
queue.append(g)
visit[g] = 1
distance[g] = distance[v]+1
answer = 0
for i in range(2, n+1) :
if len(graph[i]) == 1 :
answer += distance[i]
if answer % 2 != 0 :
print("Yes")
else :
print("No")
bfs(1, graph)