From 1ad1d8d5a70c875680e6c7df56d7b045288e8520 Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Fri, 6 Nov 2020 15:52:24 +0800 Subject: [PATCH] =?UTF-8?q?P4913=20=E3=80=90=E6=B7=B1=E5=9F=BA16.=E4=BE=8B?= =?UTF-8?q?3=E3=80=91=E4=BA=8C=E5=8F=89=E6=A0=91=E6=B7=B1=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R41386907 --- problem/P4913/P4913.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 problem/P4913/P4913.cpp diff --git a/problem/P4913/P4913.cpp b/problem/P4913/P4913.cpp new file mode 100644 index 00000000..459502e1 --- /dev/null +++ b/problem/P4913/P4913.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +int n, l[1000005], r[1000005]; + +int dfs(int now, int cnt) { + if (l[now] == 0 && r[now] == 0) { + return cnt; + } + else if (l[now] == 0) { + return dfs(r[now], cnt + 1); + } + else if (r[now] == 0) { + return dfs(l[now], cnt + 1); + } + return max(dfs(l[now], cnt + 1), dfs(r[now], cnt + 1)); +} + +int main() { + cin >> n; + for (int i = 1; i <= n; i++) { + cin >> l[i] >> r[i]; + } + cout << dfs(1, 1) << endl; + return 0; +}