defTopologicalSort(self, n, m, edges): indegrees = [0]*(n+1) visited = [0]*(n+1) dst = [[] for _ inrange(n+1)] path = [] queue = [] for edge in edges: indegrees[edge[1]] += 1 dst[edge[0]].append(edge[1]) for node inrange(1, n+1): if indegrees[node] == 0: path.append(node) visited[node] = 1 queue.append(node)
while queue: node = queue.pop(0) for d in dst[node]: ifnot visited[d]: queue.append(d) indegrees[d] -= 1 ifnot indegrees[d]: path.append(d) visited[d] = 1 # 输出 for i inrange(n): print(path[i], end=' ') return
if __name__=="__main__": # 单行输入 n, m = map(lambda x: int(x), input().split())
# 多行输入 edges = [] for i inrange(m): edges.append(list(map(lambda x: int(x), input().split())))
# 输出 Solution().TopologicalSort(n, m, edges)
Python
Python基础
python多线程,多进程 python的GIL 生成器与迭代器 装饰器 深浅拷贝 do while与while do循环语句的区别 多元赋值机制