classSolution: defbuildMatrix(self, k: int, rowConditions: List[List[int]], colConditions: List[List[int]]) -> List[List[int]]: deftopo_sort(edges: List[List[int]]) -> List[int]: g = [[] for _ inrange(k)] in_deg = [0] * k for x, y in edges: g[x - 1].append(y - 1) # 顶点编号从 0 开始,方便计算 in_deg[y - 1] += 1 order = [] q = deque(i for i, d inenumerate(in_deg) if d == 0) while q: x = q.popleft() order.append(x) for y in g[x]: in_deg[y] -= 1 if in_deg[y] == 0: q.append(y) return order iflen(order) == k elseNone
if (row := topo_sort(rowConditions)) isNoneor (col := topo_sort(colConditions)) isNone: return [] pos = {x: i for i, x inenumerate(col)} ans = [[0] * k for _ inrange(k)] for i, x inenumerate(row): ans[i][pos[x]] = x + 1 return ans
classSolution { int[] topoSort(int k, int[][] edges) { List<Integer>[] g = newArrayList[k]; Arrays.setAll(g, e -> newArrayList<>()); varinDeg=newint[k]; for (var e : edges) { intx= e[0] - 1, y = e[1] - 1; // 顶点编号从 0 开始,方便计算 g[x].add(y); ++inDeg[y]; }
varorder=newArrayList<Integer>(); varq=newArrayDeque<Integer>(); for (vari=0; i < k; ++i) if (inDeg[i] == 0) q.push(i); while (!q.isEmpty()) { varx= q.pop(); order.add(x); for (var y : g[x]) if (--inDeg[y] == 0) q.push(y); } return order.stream().mapToInt(x -> x).toArray(); }
publicint[][] buildMatrix(int k, int[][] rowConditions, int[][] colConditions) { int[] row = topoSort(k, rowConditions), col = topoSort(k, colConditions); if (row.length < k || col.length < k) returnnewint[][]{}; varpos=newint[k]; for (vari=0; i < k; ++i) pos[col[i]] = i; varans=newint[k][k]; for (vari=0; i < k; ++i) ans[i][pos[row[i]]] = row[i] + 1; return ans; } }
classSolution { vector<int> topo_sort(int k, vector<vector<int>> &edges){ vector<vector<int>> g(k); vector<int> in_deg(k); for (auto &e : edges) { int x = e[0] - 1, y = e[1] - 1; // 顶点编号从 0 开始,方便计算 g[x].push_back(y); ++in_deg[y]; }
vector<int> order; queue<int> q; for (int i = 0; i < k; ++i) if (in_deg[i] == 0) q.push(i); while (!q.empty()) { int x = q.front(); q.pop(); order.push_back(x); for (int y : g[x]) if (--in_deg[y] == 0) q.push(y); } return order; }
public: vector<vector<int>> buildMatrix(int k, vector<vector<int>> &rowConditions, vector<vector<int>> &colConditions) { auto row = topo_sort(k, rowConditions), col = topo_sort(k, colConditions); if (row.size() < k || col.size() < k) return {}; vector<int> pos(k); for (int i = 0; i < k; ++i) pos[col[i]] = i; vector<vector<int>> ans(k, vector<int>(k)); for (int i = 0; i < k; ++i) ans[i][pos[row[i]]] = row[i] + 1; return ans; } };
functopoSort(k int, edges [][]int) []int { g := make([][]int, k) inDeg := make([]int, k) for _, e := range edges { x, y := e[0]-1, e[1]-1// 顶点编号从 0 开始,方便计算 g[x] = append(g[x], y) inDeg[y]++ } q := make([]int, 0, k) orders := q // 复用队列作为拓扑序 for i, d := range inDeg { if d == 0 { q = append(q, i) } } forlen(q) > 0 { x := q[0] q = q[1:] for _, y := range g[x] { if inDeg[y]--; inDeg[y] == 0 { q = append(q, y) } } } ifcap(q) > 0 { returnnil } return orders[:k] }
funcbuildMatrix(k int, rowConditions, colConditions [][]int) [][]int { row := topoSort(k, rowConditions) col := topoSort(k, colConditions) if row == nil || col == nil { returnnil } pos := make([]int, k) for i, v := range col { pos[v] = i } ans := make([][]int, k) for i, x := range row { ans[i] = make([]int, k) ans[i][pos[x]] = x + 1 } return ans }