import sys
def push(x):
stack.append(x)
def pop():
if stack:
return stack.pop()
else:
return -1
def size():
return len(stack)
def empty():
if stack:
return 0
else:
return 1
def top():
try:
return stack[-1]
except:
return -1
n = int(sys.stdin.readline().rstrip())
stack = []
for _ in range(n):
i = sys.stdin.readline().rstrip().split()
a = i[0]
if a == 'push':
push(i[1])
elif a == 'pop':
print(pop())
elif a == 'size':
print(size())
elif a == 'empty':
print(empty())
elif a == 'top':
print(top())
sys.stdin.readline()
사용안하면 시간초과뜬다.def empty():
if stack:
return 0
else:
return 1
를
def empty():
return 0 if slack else 1
로 쓰는 게 더욱 깔끔하다.
bfs를 이용했다. solution을 참고했다.
def bfs(queue):
global result
#큐가 빌때까지 반복
while queue:
second_queue = []
temp_result = 0
while queue:
#꺼내서 사용하고 사용체크
temp = queue.pop()
use_check[temp] = True
#현재 전파상태에서 최댓값 찾기
if temp > temp_result:
temp_result = temp
#연결되어있는부분 큐에 넣기
while connect_list[temp]:
s_temp = connect_list[temp].pop()
#이미 들른곳이라면 패스
if use_check[s_temp]: continue
second_queue.append(s_temp)
#가야할 장소가 있다면 bfs다시 돌리기
if second_queue:
queue = second_queue
#큐가 비어있다면 마지막 전파단계이다.
result = temp_result
for t in range(1):
connect_list = [set() for _ in range(101)]
use_check = [False] * 101
length, start = map(int, input().split())
temp_list = list(map(int, input().split()))
for i in range(0, len(temp_list), 2):
connect_list[temp_list[i]].add(temp_list[i+1])
result = 0
bfs([start])
print('#{} {}'.format(t+1, result))