forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
47 lines (47 loc) · 1.23 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
class Solution {
public:
bool sequenceReconstruction(vector<int>& org, vector<vector<int>>& seqs) {
int n = org.size();
unordered_set<int> nums;
for (auto& seq : seqs)
{
for (int num : seq)
{
if (num < 1 || num > n) return false;
nums.insert(num);
}
}
if (nums.size() < n) return false;
vector<vector<int>> edges(n + 1);
vector<int> indegree(n + 1);
for (auto& seq : seqs)
{
int i = seq[0];
for (int j = 1; j < seq.size(); ++j)
{
edges[i].push_back(seq[j]);
++indegree[seq[j]];
i = seq[j];
}
}
queue<int> q;
for (int i = 1; i <= n; ++i)
{
if (indegree[i] == 0) q.push(i);
}
int cnt = 0;
while (!q.empty())
{
if (q.size() > 1 || q.front() != org[cnt]) return false;
++cnt;
int i = q.front();
q.pop();
for (int j : edges[i])
{
--indegree[j];
if (indegree[j] == 0) q.push(j);
}
}
return cnt == n;
}
};