-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2392.cpp
More file actions
45 lines (43 loc) · 1.5 KB
/
2392.cpp
File metadata and controls
45 lines (43 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {
public:
vector<int> topological(vector<vector<int>>& orders, int k) {
vector<int> inDegress(k, 0);
vector<vector<int>> adjacency(k);
for (auto& order : orders) {
inDegress[order[1] - 1]++;
adjacency[order[0] - 1].push_back(order[1] - 1);
}
queue<int> q;
for (int i = 0; i < k; ++i) {
if (inDegress[i] == 0) q.push(i);
}
vector<int> res;
while (!q.empty()) {
int node = q.front();
q.pop();
res.push_back(node);
for (auto& neighbor : adjacency[node]) {
inDegress[neighbor]--;
if (inDegress[neighbor] == 0) q.push(neighbor);
}
}
return res;
}
vector<vector<int>> buildMatrix(int k, vector<vector<int>>& rowConditions, vector<vector<int>>& colConditions) {
vector<int> rowOrder = topological(rowConditions, k);
vector<int> colOrder = topological(colConditions, k);
if (rowOrder.size() != k) return {};
if (colOrder.size() != k) return {};
vector<int> rowPosition(k, 0);
vector<int> colPosition(k, 0);
for (int i = 0; i < k; ++i) {
rowPosition[rowOrder[i]] = i;
colPosition[colOrder[i]] = i;
}
vector<vector<int>> matrix(k, vector<int>(k, 0));
for (int i = 0; i < k; ++i) {
matrix[rowPosition[i]][colPosition[i]] = i + 1;
}
return matrix;
}
};