-
Notifications
You must be signed in to change notification settings - Fork 3
/
444.cpp
44 lines (43 loc) · 1.41 KB
/
444.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
class Solution {
public:
bool sequenceReconstruction(vector<int>& nums, vector<vector<int>>& sequences) {
vector<vector<int>> adjacency(10001);
vector<int> inDegrees(10001, 0);
vector<bool> existed(10001, false);
for (auto& sequence : sequences) {
int n = sequence.size();
existed[sequence[0]] = true;
for (int i = 1; i < n; ++i) {
adjacency[sequence[i - 1]].push_back(sequence[i]);
inDegrees[sequence[i]]++;
existed[sequence[i]] = true;
}
}
vector<int> res;
queue<int> q;
for (int i = 0; i < 10001; ++i) {
if (existed[i] && inDegrees[i] == 0) q.push(i);
}
if (q.size() > 1) return false;
while (!q.empty()) {
int node = q.front();
res.push_back(node);
q.pop();
int count = 0;
for (auto& neighbor : adjacency[node]) {
inDegrees[neighbor]--;
if (inDegrees[neighbor] == 0) {
q.push(neighbor);
count++;
if (count > 1) return false;
}
}
}
if (res.size() != nums.size()) return false;
int m = res.size();
for (int i = 0; i < m; ++i) {
if (res[i] != nums[i]) return false;
}
return true;
}
};