https://www.acmicpc.net/problem/15805
파이썬 코드
#15805_트리나라관광가이드
from collections import Counter
n=int(input())
city=list(map(int,input().split()))
cntcity=Counter(city)
visited=[0 for _ in range(len(cntcity.keys()))]
parent=[0 for _ in range(len(cntcity.keys()))]
parent[city[0]]=-1
tree=[city[0]]
for i in range(n):
if visited[city[i]]==0:
if i<=0:
parent[city[i]]=-1
else:
parent[city[i]]=city[i-1]
tree.append(city[i])
visited[city[i]]=1
print(len(cntcity.keys()))
for i in parent:
print(i, end=' ')
'알고리즘 > 백준(BOJ)' 카테고리의 다른 글
[백준/BOJ]#2078:무한이진트리[트리/수학/파이썬/python] (0) | 2022.04.09 |
---|---|
[백준/BOJ]#1406:에디터[스택/파이썬/python] (0) | 2022.04.08 |
[백준/BOJ]#9935:문자열 폭발[문자열/스택/파이썬/python] (0) | 2022.02.14 |
[백준/BOJ]#2263:트리의 순회 [트리/분할정복/파이썬/python] (0) | 2022.02.14 |
[백준/BOJ/종만북/알고스팟]#1922:쿼드트리/QUADTREE [분할정복/파이썬/python] (0) | 2022.01.28 |