0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-27 15:36:27 +00:00

F - Pre-order and In-order

https://atcoder.jp/contests/abc255/submissions/32427435
This commit is contained in:
Baoshuo Ren 2022-06-12 08:50:40 +08:00
parent 46e16f5913
commit 203a82bb11
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

58
AtCoder/ABC255/F/F.cpp Normal file
View File

@ -0,0 +1,58 @@
#include <iostream>
#include <algorithm>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 2e5 + 5;
int n, cnt, pre[N], in[N];
std::pair<int, int> tr[N];
int dfs(int l, int r) {
if (l > r) return 0;
if (cnt > n) {
cout << -1 << endl;
exit(0);
}
int root = pre[++cnt];
tr[root].first = dfs(l, in[root] - 1);
tr[root].second = dfs(in[root] + 1, r);
return root;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> pre[i];
}
for (int i = 1, x; i <= n; i++) {
cin >> x;
in[x] = i;
}
if (pre[1] != 1) {
cout << -1 << endl;
exit(0);
}
dfs(1, n);
for (int i = 1; i <= n; i++) {
cout << tr[i].first << ' ' << tr[i].second << endl;
}
return 0;
}