From 0a53ccaae462ede57f94c01a6dbb8cbbaaab8166 Mon Sep 17 00:00:00 2001 From: sh0723 Date: Wed, 3 Jun 2026 16:40:19 +0900 Subject: [PATCH 1/2] =?UTF-8?q?1068=20:=20=ED=8A=B8=EB=A6=AC=20v1=20solve?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/week2/BOJ1068/BOJ1068_V1.cpp | 55 ++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/week2/BOJ1068/BOJ1068_V1.cpp diff --git a/src/week2/BOJ1068/BOJ1068_V1.cpp b/src/week2/BOJ1068/BOJ1068_V1.cpp new file mode 100644 index 0000000..b5a604c --- /dev/null +++ b/src/week2/BOJ1068/BOJ1068_V1.cpp @@ -0,0 +1,55 @@ +#include +using namespace std; +int target; +int N; +vector> tree; +int cnt = 0; +void dfs(int num) { + if (tree[num].size() == 0) { + cnt++; + return; + } + + for (int i : tree[num]) { + dfs(i); + } +} + +int main() { + + cin >> N; + for (int i = 0; i < N; i++) { + tree.push_back(vector()); + int num; + cin >> num; + if (num == -1) continue; + tree[num].push_back(i); + } + + cin >> target; + if (target == 0) { + cout << 0; + return 0; + } + + tree[target].clear(); + int num = 0; + int flag = true; + while(flag) { + if (num == tree.size()) + break; + for (int i=0; i Date: Wed, 3 Jun 2026 16:52:51 +0900 Subject: [PATCH 2/2] =?UTF-8?q?1068=20:=20=ED=8A=B8=EB=A6=AC=20v2=20solve?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/week2/BOJ1068/BOJ1068_V2.cpp | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/week2/BOJ1068/BOJ1068_V2.cpp diff --git a/src/week2/BOJ1068/BOJ1068_V2.cpp b/src/week2/BOJ1068/BOJ1068_V2.cpp new file mode 100644 index 0000000..40ada7e --- /dev/null +++ b/src/week2/BOJ1068/BOJ1068_V2.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; +int target; +int N; +vector> tree; +int dfs(int num) { + int ret = 0; + int child = 0; + for (int i : tree[num]) { + if (i == target) continue; + ret += dfs(i); + child++; + } + + if (child == 0) + return 1; + + return ret; +} + +int main() { + + cin >> N; + for (int i = 0; i < N; i++) { + tree.push_back(vector()); + int num; + cin >> num; + if (num == -1) continue; + tree[num].push_back(i); + } + + cin >> target; + if (target == 0) { + cout << 0; + return 0; + } + + + cout << dfs(0); + + return 0; +}