拓扑排序实现原理
https://blog.csdn.net/dm_vincent/article/details/7714519
在网上看到一个很惊艳的解法,里面有很多小check
class Solution(object):
def canFinish(self, n, pres):
from collections import deque
ind = [[] for _ in xrange(n)] # indegree #关系举证
oud = [0] * n # outdegree #出度
for p in pres:
oud[p[0]] += 1 #出度加一
ind[p[1]].append(p[0]) #关系加一
dq = deque()
for i in xrange(n):
if oud[i] == 0:
dq.append(i)
k = 0
while dq:
x = dq.popleft()
k += 1
for i in ind[x]:
oud[i] -= 1 #只有出度相减才看是不是要算出度
if oud[i] == 0:
dq.append(i)
return k == n