-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path514.cpp
More file actions
46 lines (45 loc) · 1.34 KB
/
514.cpp
File metadata and controls
46 lines (45 loc) · 1.34 KB
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
#include <bits/stdc++.h>
using namespace std;
int main() {
stack<int> s;
queue<int> q;
queue<int> parent;
int i, j, k, l, n, t = 0;
while (scanf("%d", &n) == 1) {
if (n == 0) break;
while (scanf("%d", &k)) {
if (k == 0) break;
parent.push(k);
for (i = 2; i <= n; i++) {
scanf("%d", &k);
parent.push(k);
}
for (i = 1; i <= n; i++) q.push(i);
int flag = 0;
while (q.size() != 0 || parent.size() != 0) {
if (q.front() == parent.front()) {
q.pop();
parent.pop();
} else if (s.size() != 0 && s.top() == parent.front()) {
s.pop();
parent.pop();
} else if (q.size() == 0 && parent.front() != s.top()) {
flag = 1;
break;
} else {
s.push(q.front());
q.pop();
}
}
if (s.size() == 0 && flag == 0)
puts("Yes");
else
puts("No");
while (q.size() != 0) q.pop();
while (s.size() != 0) s.pop();
while (parent.size() != 0) parent.pop();
}
printf("\n");
}
return 0;
}