Repository navigation
Expand file tree
/
Copy path1466.cpp
More file actions
90 lines (76 loc) · 1.37 KB
/
Copy path1466.cpp
File metadata and controls
90 lines (76 loc) · 1.37 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<bits/stdc++.h>
using namespace std;
struct node {
int val;
int l;
int r;
node () {
val = l = r = -1;
}
node (int x) {
val = x;
l = r = -1;
}
};
void insert(int i, int val, vector<node> &G) {
if (val < G[i].val) {
if (G[i].l == -1) {
int id = G.size();
G[i].l = id;
G.emplace_back(val);
return;
}
else {
insert(G[i].l, val, G);
}
}
else {
if (G[i].r == -1) {
int id = G.size();
G[i].r = id;
G.emplace_back(val);
return;
}
else {
insert(G[i].r, val, G);
}
}
}
void solve(vector<node> G) {
queue<node> Q;
Q.push(G[0]);
vector<int> ans;
while (!Q.empty()) {
node cur = Q.front(); Q.pop();
ans.push_back(cur.val);
if (cur.l!= -1) {
Q.push(G[cur.l]);
}
if (cur.r!= -1) {
Q.push(G[cur.r]);
}
}
for (int i = 0; i < ans.size(); ++i) {
if (i > 0) cout << " ";
cout << ans[i];
}
}
int main() {
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
int n, r;
cin >> n >> r;
vector<node> G;
G.emplace_back(r);
for (int j = 1; j < n; ++j) {
int a;
cin >> a;
insert(0, a, G);
}
cout << "Case " << i + 1 << ":" << endl;
solve(G);
cout << endl << endl;
}
return 0;
}