From 203a82bb11e5a8f4e42d6620d78c7c4aaf4929de Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sun, 12 Jun 2022 08:50:40 +0800 Subject: [PATCH] F - Pre-order and In-order https://atcoder.jp/contests/abc255/submissions/32427435 --- AtCoder/ABC255/F/F.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 AtCoder/ABC255/F/F.cpp diff --git a/AtCoder/ABC255/F/F.cpp b/AtCoder/ABC255/F/F.cpp new file mode 100644 index 00000000..597797d5 --- /dev/null +++ b/AtCoder/ABC255/F/F.cpp @@ -0,0 +1,58 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 2e5 + 5; + +int n, cnt, pre[N], in[N]; +std::pair 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; +}