Most efficient solution using Python
\n\n# Code\n\nclass Solution(object):\n    def buildMatrix(self, k, rowConditions, colConditions):\n        rowGraph = defaultdict(list)\n        for u, v in ro
\n\n# Code\n```\nclass Solution(object):\n    def buildMatrix(self, k, rowConditions, colConditions):\n        rowGraph = defaultdict(list)\n        for u, v in rowConditions:\n            rowGraph[u].append(v)\n            \n        colGraph = defaultdict(list)\n        for u, v in colConditions:\n            colGraph[u].append(v)\n        \n        def topoSort(graph):\n            inDegree = {i: 0 for i in range(1, k + 1)}\n            for u in graph:\n                for v in graph[u]:\n                    inDegree[v] += 1\n            queue = deque([i for i in inDegree if inDegree[i] == 0])\n            order = []\n            while queue:\n                node = queue.popleft()\n                order.append(node)\n                for v in graph[node]:\n                    inDegree[v] -= 1\n                    if inDegree[v] == 0:\n                        queue.append(v)\n            return order if len(order) == k else []\n        \n        rowOrder = topoSort(rowGraph)\n        colOrder = topoSort(colGraph)\n        \n        if not rowOrder or not colOrder:\n            return []\n        \n        rowMap = {num: i for i, num in enumerate(rowOrder)}\n        colMap = {num: i for i, num in enumerate(colOrder)}\n        \n        result = [[0] * k for _ in range(k)]\n        for i in range(1, k + 1):\n            result[rowMap[i]][colMap[i]] = i\n        \n        return result\n        \n```
['Python3']