forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
50 lines (48 loc) · 1.37 KB
/
Solution.cpp
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
46
47
48
49
50
class Solution {
public:
int k;
vector<vector<int>> buildMatrix(int k, vector<vector<int>>& rowConditions, vector<vector<int>>& colConditions) {
this->k = k;
auto row = f(rowConditions);
auto col = f(colConditions);
if (row.empty() || col.empty()) return {};
vector<vector<int>> ans(k, vector<int>(k));
vector<int> m(k + 1);
for (int i = 0; i < k; ++i) {
m[col[i]] = i;
}
for (int i = 0; i < k; ++i) {
ans[i][m[row[i]]] = row[i];
}
return ans;
}
vector<int> f(vector<vector<int>>& cond) {
vector<vector<int>> g(k + 1);
vector<int> indeg(k + 1);
for (auto& e : cond) {
int a = e[0], b = e[1];
g[a].push_back(b);
++indeg[b];
}
queue<int> q;
for (int i = 1; i < k + 1; ++i) {
if (!indeg[i]) {
q.push(i);
}
}
vector<int> res;
while (!q.empty()) {
for (int n = q.size(); n; --n) {
int i = q.front();
res.push_back(i);
q.pop();
for (int j : g[i]) {
if (--indeg[j] == 0) {
q.push(j);
}
}
}
}
return res.size() == k ? res : vector<int>();
}
};