题解 | #比赛名次#python
比赛名次
https://www.nowcoder.com/practice/a3a561d688264a8baa31b3edf2610641
while 1: try: N,M=list(map(int,input().split())) sons=[[] for i in range(N+1)] # 记录每个队的手下败将 dft=[0 for i in range(N+1)] # 记录每个队的败场数 for i in range(M): a,b=list(map(int,input().split())) sons[a].append(b) dft[b]+=1 fath=[] # 记录未尝一败的队伍 res=[] for i in range(1,N+1): if dft[i]==0: fath.append(i) while fath: m=min(fath) res.append(m) # 记录未尝一败的队伍中最小值 fath.remove(m) # 拓扑网中去掉该队伍 for son in sons[m]: dft[son]-=1 # 该队伍的手下败将败场数-1 if dft[son]==0: fath.append(son) print(*res) except Exception as e: break