forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
31 lines (31 loc) · 834 Bytes
/
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
class Solution {
public:
bool sequenceReconstruction(vector<int>& nums, vector<vector<int>>& sequences) {
int n = nums.size();
vector<int> indeg(n);
vector<int> g[n];
for (auto& seq : sequences) {
for (int i = 1; i < seq.size(); ++i) {
int a = seq[i - 1] - 1, b = seq[i] - 1;
g[a].push_back(b);
++indeg[b];
}
}
queue<int> q;
for (int i = 0; i < n; ++i) {
if (indeg[i] == 0) {
q.push(i);
}
}
while (q.size() == 1) {
int i = q.front();
q.pop();
for (int j : g[i]) {
if (--indeg[j] == 0) {
q.push(j);
}
}
}
return q.empty();
}
};