def tp_sort(self, items, indegree, neighbors):
q = collections.deque([])
for neighbor in neighbors[cur]:
if not indegree[neighbor]:
def sortItems(self, n: int, m: int, group: List[int], pres: List[List[int]]) -> List[int]:
group[project] = max_group_id
project_indegree = collections.defaultdict(int)
group_indegree = collections.defaultdict(int)
project_neighbors = collections.defaultdict(list)
group_neighbors = collections.defaultdict(list)
group_projects = collections.defaultdict(list)
group_projects[group[project]].append(project)
for pre in pres[project]:
if group[pre] != group[project]:
group_indegree[group[project]] += 1
group_neighbors[group[pre]].append(group[project])
project_indegree[project] += 1
project_neighbors[pre].append(project)
group_queue = self.tp_sort([i for i in range(max_group_id)], group_indegree, group_neighbors)
if len(group_queue) != max_group_id:
for group_id in group_queue:
project_queue = self.tp_sort(group_projects[group_id], project_indegree, project_neighbors)
if len(project_queue) != len(group_projects[group_id]):