-
Notifications
You must be signed in to change notification settings - Fork 111
/
sequence-reconstruction.cc
43 lines (42 loc) · 1.01 KB
/
sequence-reconstruction.cc
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
// Sequence Reconstruction
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
class Solution {
public:
bool sequenceReconstruction(vector<int>& org, vector<vector<int>>& seqs) {
int n = org.size();
vector<int> d(n, 0);
vector<char> v(n, 0);
vector<vector<int>> e(n);
for (auto& seq: seqs) {
for (int i: seq) {
if (i <= 0 || i > n)
return false;
v[i-1] = 1;
}
REP(i, int(seq.size())-1) {
e[seq[i]-1].push_back(seq[i+1]-1);
d[seq[i+1]-1]++;
}
}
if (count(v.begin(), v.end(), 0))
return false;
int r = 0, l = 0, x = -1;
REP(i, n)
if (! d[i]) {
if (x >= 0) return false;
x = i;
}
while (x >= 0) {
if (x != org[l++]-1) return false;
int u = x;
x = -1;
for (int y: e[u])
if (! --d[y]) {
if (x >= 0) return false;
x = y;
}
}
return l == n;
}
};