class Solution(object):
def minReorder(self, n, connections):
"""
:type n: int
:type connections: List[List[int]]
:rtype: int
"""
# 记录所有点的出、入度:0表示入度,1表示出度
edge = [[] for _ in range(n)]
# 遍历参数中的边,每条边的两个端点都要记录相应的出、入度
for start, end in connections:
edge[start].append((end, 1))
edge[end].append((start, 0))
print(edge)
# 设置每个点的访问标记,避免重复访问;对于本题,0点初始化是已访问的
visit = [False] * n
visit[0] = True
# 返回值
ans = 0
# 开始BFS
already = [0]
while already:
cur = already.pop(0)
print(visit, edge[cur])
for index, degree in edge[cur]:
if not visit[index]:
visit[index] = True
ans += degree
already.append(index)
return ans
if __name__ == '__main__':
s = Solution()
print(s.minReorder(5, [[4, 3], [2, 3], [1, 2], [1, 0]]))