AcWing 1191. 家谱树
原题链接
简单
作者:
叶枝黎曼
,
2020-11-01 19:12:53
,
所有人可见
,
阅读 388
拓扑排序
一道裸的拓扑排序(我爱BFS)
python版本
#1191. 家谱树
def topSort():
queue = []
for i in range(1,n+1):
if ind[i] == 0:
queue.append(i)
while queue:
sam = queue.pop(0)
res.append(sam)
for node in link[sam]:
ind[node] -= 1
if ind[node] == 0:
queue.append(node)
n = int(input())
ind = [0]*(n+1)
link = {}
for i in range(n):
temp = [int(i) for i in input().split()]
del temp[-1]
link[i+1] = set()
for var in temp:
link[i+1].add(var)
ind[var] += 1
res = []
topSort()
print(' '.join(map(str,res)))