1. Problem
2. My Solution
import sys
import math
from collections import deque
def bfs(pos):
global res
queue = deque()
queue.append((pos,0))
position[pos] = -1 # 방문 표시
while(queue):
pos, move_cnt = queue.popleft()
position[pos] = -1
move = [-1,1,pos]
if pos == k
res = move_cnt # 현재 res 보다 move_cnt 가 작다면
for dx in move:
next_pos = pos + dx
if 0 <= next_pos <= 100000 and position[next_pos] != -1:
queue.append((next_pos, move_cnt+1))
position[next_pos] = -1
n,k = map(int,sys.stdin.readline().rstrip().split())
position = [False] * (100001)
res = math.inf
if n == k:
print(0)
exit()
elif n+1 == k or n-1 == k or 2*n ==k:
print(1)
exit()
bfs(n)
print(res)
1. Problem
2. Others' Solutions
import sys
# 중위 순회 함수
def inorder(node, level):
global order
left,right = graph[node].values()
if left != -1:
inorder(left,level+1)
order += 1
level_distance[level].append(order)
if right != -1:
inorder(right,level+1)
# 너비가 가장 큰 레벨과 너비를 찾는 함수
def find_level_distance():
width = 0
for i in range(1,n+1):
if level_distance[i]:
temp = max(level_distance[i]) - min(level_distance[i]) + 1
if temp > width:
level = i
width = temp
return level, width
n = int(sys.stdin.readline())
graph = {}
parent_check = [False] * n
level_distance = [[] for _ in range(n+1)]
order = 0
for _ in range(n):
node,left,right = map(int,sys.stdin.readline().rstrip().split())
graph[node] = {'l': left, 'r':right}
# 해당 조건문을 적용하지 않으면 parent_check[-2]는 무조건 True
if left != -1:
parent_check[left-1] = True
if right != -1:
parent_check[right-1] = True
# 부모가 없는 노드 = root 노드
root = parent_check.index(False) + 1
inorder(root,1)
print(*find_level_distance())
3. Learned
ex)
graph[node] = {'l': left, 'r':right}
left,right = graph[node].values() -> left, right 값 출력
1. Problem
2. Others' Solutions
import sys
sys.setrecursionlimit(10**8)
def dfs(node, distance, visited):
global max_distance
global max_distance_node
visited[node] = True
if max_distance < distance:
max_distance = distance
max_distance_node = node
for u,d in tree[node]:
if visited[u] == False:
dfs(u,distance + d, visited)
n = int(sys.stdin.readline())
tree = [[] for _ in range(n+1)]
visited = [False] * (n+1)
max_distance_node = None
max_distance = 0
for _ in range(1,n+1):
inf = list(map(int,sys.stdin.readline().rstrip().split()))
idx = inf.pop(0)
while len(inf) != 1:
v = inf.pop(0)
d = inf.pop(0)
tree[idx].append((v,d))
dfs(1,0,visited.copy())
dfs(max_distance_node,0,visited.copy())
print(max_distance)
import sys
from collections import deque
sys.setrecursionlimit(10**8)
def dfs(node, distance, visited):
global max_distance
global max_distance_node
visited[node] = True
if max_distance < distance:
max_distance = distance
max_distance_node = node
for u,d in tree[node]:
if visited[u] == False:
dfs(u,distance + d, visited)
n = int(sys.stdin.readline())
tree = [[] for _ in range(n+1)]
visited = [False] * (n+1)
max_distance_node = None
max_distance = 0
for _ in range(1,n+1):
inf = deque(list(map(int,sys.stdin.readline().rstrip().split())))
idx = inf.popleft()
while len(inf) != 1:
v = inf.popleft()
d = inf.popleft()
tree[idx].append((v,d))
dfs(1,0,visited.copy())
dfs(max_distance_node,0,visited.copy())
print(max_distance)
3. Learned