From 5b42e79b5e2ce685cb2cee361139212c176d0b03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EB=AC=B8=ED=98=95?= <74577714+alirz-pixel@users.noreply.github.com> Date: Wed, 21 Jan 2026 00:35:34 +0900 Subject: [PATCH] =?UTF-8?q?260120=20:=20[BOJ=2022856]=20=ED=8A=B8=EB=A6=AC?= =?UTF-8?q?=20=EC=88=9C=ED=9A=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _munhyeong/22856.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 _munhyeong/22856.cpp diff --git a/_munhyeong/22856.cpp b/_munhyeong/22856.cpp new file mode 100644 index 00000000..41985fda --- /dev/null +++ b/_munhyeong/22856.cpp @@ -0,0 +1,75 @@ +#include +#include +#include + +using namespace std; + +int find_end_node(vector>& nodes) { + int cur_top = 0; + while (true) { + // 1. 단 오른쪽으로 가기 + if (nodes[cur_top].second >= 0) + cur_top = nodes[cur_top].second; + else + break; + } + return cur_top; +} + +int dfs(vector> &nodes) { + int end_node = find_end_node(nodes); + //cout << end_node + 1 << "\n"; + + vector visited(nodes.size()); + stack st; + st.push(0); + + int answer = 0; + while (!st.empty()) { + int top = st.top(); + st.pop(); + + //cout << top + 1 << " "; + answer++; + + bool is_inserted = false; + + if (nodes[top].second >= 0 && !visited[nodes[top].second]) { + visited[nodes[top].second] = true; + st.push(top); + st.push(nodes[top].second); + is_inserted = true; + } + + if (nodes[top].first >= 0 && !visited[nodes[top].first]) { + visited[nodes[top].first] = true; + st.push(top); + st.push(nodes[top].first); + is_inserted = true; + } + + if (!is_inserted) { + if (top == end_node) + break; + } + } + return answer - 1; +} + +int main() { + int N; + cin >> N; + + vector> nodes(N); + for (int i = 0; i < N; i++) { + int a, b, c; + cin >> a >> b >> c; + a--; b--; c--; + + nodes[a] = { b, c }; + } + + cout << dfs(nodes); + + return 0; +}