No one can give such quality code... Bests 98.9% users.... #Quality code
\n\n# Python\n\nclass Solution:\n\n    def buildMatrix(\n\n        self,\n\n        k: int,\n\n        rowConditions: List[List[int]],\n\n        colConditions:
\n\n# Python\n```\nclass Solution:\n\n    def buildMatrix(\n\n        self,\n\n        k: int,\n\n        rowConditions: List[List[int]],\n\n        colConditions: List[List[int]],\n\n    ) -> List[List[int]]:\n\n        # Store the topologically sorted sequences.\n\n        order_rows = self.__topoSort(rowConditions, k)\n\n        order_columns = self.__topoSort(colConditions, k)\n\n        # If no topological sort exists, return empty array.\n\n        if not order_rows or not order_columns:\n\n            return []\n\n        matrix = [[0] * k for _ in range(k)]\n\n        pos_row = {num: i for i, num in enumerate(order_rows)}\n\n        pos_col = {num: i for i, num in enumerate(order_columns)}\n\n        for num in range(1, k + 1):\n\n            if num in pos_row and num in pos_col:\n\n                matrix[pos_row[num]][pos_col[num]] = num\n\n        return matrix\n\n    def __topoSort(self, edges: List[List[int]], n: int) -> List[int]:\n\n        adj = defaultdict(list)\n\n        order = []\n\n        visited = [0] * (n + 1)\n\n        has_cycle = [False]\n\n        # Build adjacency list\n\n        for x, y in edges:\n\n            adj[x].append(y)\n\n        # Perform DFS for each node\n\n        for i in range(1, n + 1):\n\n            if visited[i] == 0:\n\n                self.__dfs(i, adj, visited, order, has_cycle)\n\n                # Return empty if cycle detected\n\n                if has_cycle[0]:\n\n                    return []\n\n        # Reverse to get the correct order\n\n        order.reverse()\n\n        return order\n\n    def __dfs(\n\n        self,\n\n        node: int,\n\n        adj: defaultdict,\n\n        visited: List[int],\n\n        order: List[int],\n\n        has_cycle: List[bool],\n\n    ):\n\n        # Mark node as visiting\n\n        visited[node] = 1\n\n        for neighbor in adj[node]:\n\n            if visited[neighbor] == 0:\n\n                self.__dfs(neighbor, adj, visited, order, has_cycle)\n\n                # Early exit if a cycle is detected\n\n                if has_cycle[0]:\n\n                    return\n\n            elif visited[neighbor] == 1:\n\n                # Cycle detected\n\n                has_cycle[0] = True\n\n                return\n\n        # Mark node as visited\n\n        visited[node] = 2\n\n        # Add node to the order\n\n        order.append(node)\n\n        \n```\n\n# C++\n```\nclass Solution {\npublic:\n    //topological sort using BFS (Kahn\'s algorithm)\n    static vector<int> topo_sort(int k, vector<vector<int>>& conditions){\n        vector<int> deg(k+1, 0);\n        vector<vector<int>> adj(k+1);\n        for(auto& edge: conditions){\n            int v=edge[0], w=edge[1];\n            adj[v].push_back(w);\n            deg[w]++;\n        }\n        queue<int> q;\n        for (int i = 1; i<=k; i++) {\n            if (deg[i] == 0) \n                q.push(i);\n        }\n\n        int count=0;\n        vector<int> ans;\n        ans.reserve(k);\n        while(!q.empty()){\n            int j=q.front();\n            q.pop();\n            ans.push_back(j);\n            count++;\n            for(int k:adj[j]){\n                deg[k]--;\n                if (deg[k] == 0) {\n                    q.push(k);\n                }\n            }\n        }\n        if(count != k) return {};// has cycle\n        else return ans ;\n\n    }\n\n    static void print(auto& c){\n        cout<<"[";\n        for(int x: c){\n            cout<<x;\n            if (x!=c.back()) cout<<", ";\n        }\n        cout<<"]\\n";\n    }\n\n    static vector<vector<int>> buildMatrix(int k, vector<vector<int>>& rowConditions, vector<vector<int>>& colConditions) {\n        vector<int> order_row=topo_sort(k, rowConditions);\n        vector<int> order_col=topo_sort(k, colConditions);\n\n    //  print out the ordering in row & in col\n    //    print(order_row), print(order_col);\n\n        if (order_row.empty()|| order_col.empty())\n            return {};// some conflict\n\n        vector<vector<int>> arr(k, vector<int>(k));\n        // Find pos for x where 1<=x<=k\n        vector<int> pos_i(k+1, -1), pos_j(k+1, -1);\n        for(int i=0; i<k; i++)\n            pos_i[order_row[i]]=i;\n\n        for(int i=0; i<k; i++)\n            pos_j[order_col[i]]=i;\n\n        for(int x=1; x<=k; x++)\n            arr[pos_i[x]][pos_j[x]]=x;\n        \n        return arr;\n    }\n};\n\n\n\n\nauto init = []() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    cout.tie(nullptr);\n    return \'c\';\n}();\n```\n\n# Java\n```\nclass Solution {\n\n    public int[][] buildMatrix(\n        int k,\n        int[][] rowConditions,\n        int[][] colConditions\n    ) {\n        // Store the topologically sorted sequences.\n        List<Integer> orderRows = topoSort(rowConditions, k);\n        List<Integer> orderColumns = topoSort(colConditions, k);\n\n        // If no topological sort exists, return empty array.\n        if (orderRows.isEmpty() || orderColumns.isEmpty()) return new int[0][0];\n\n        int[][] matrix = new int[k][k];\n        for (int i = 0; i < k; i++) {\n            for (int j = 0; j < k; j++) {\n                if (orderRows.get(i).equals(orderColumns.get(j))) {\n                    matrix[i][j] = orderRows.get(i);\n                }\n            }\n        }\n        return matrix;\n    }\n\n    private List<Integer> topoSort(int[][] edges, int n) {\n        // Build adjacency list\n        List<List<Integer>> adj = new ArrayList<>();\n        for (int i = 0; i <= n; i++) {\n            adj.add(new ArrayList<>());\n        }\n        for (int[] edge : edges) {\n            adj.get(edge[0]).add(edge[1]);\n        }\n\n        List<Integer> order = new ArrayList<>();\n        // 0: not visited, 1: visiting, 2: visited\n        int[] visited = new int[n + 1];\n        boolean[] hasCycle = { false };\n\n        // Perform DFS for each node\n        for (int i = 1; i <= n; i++) {\n            if (visited[i] == 0) {\n                dfs(i, adj, visited, order, hasCycle);\n                // Return empty if cycle detected\n                if (hasCycle[0]) return new ArrayList<>();\n            }\n        }\n\n        // Reverse to get the correct order\n        Collections.reverse(order);\n        return order;\n    }\n\n    private void dfs(\n        int node,\n        List<List<Integer>> adj,\n        int[] visited,\n        List<Integer> order,\n        boolean[] hasCycle\n    ) {\n        visited[node] = 1; // Mark node as visiting\n        for (int neighbor : adj.get(node)) {\n            if (visited[neighbor] == 0) {\n                dfs(neighbor, adj, visited, order, hasCycle);\n                // Early exit if a cycle is detected\n                if (hasCycle[0]) return;\n            } else if (visited[neighbor] == 1) {\n                // Cycle detected\n                hasCycle[0] = true;\n                return;\n            }\n        }\n        // Mark node as visited\n        visited[node] = 2;\n        // Add node to the order\n        order.add(node);\n    }\n}\n```
['Array', 'Graph', 'Topological Sort', 'C', 'Matrix', 'Python', 'Java', 'Python3', 'JavaScript']