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; +}