#include <stdio.h> #include <iostream> #include <vector> #include <map> using namespace std; vector<int> graph[55]; int N; int rootNode; int delNode; int leafCnt; void find_leaf(int node) { if(graph[node].size() == 0) { leafCnt++; } for(int i = 0; i < graph[node].size(); i++) { // 현재노드의 자식이 삭제할 노드이고, if(graph[node][i] == delNode) { // 현재노드의 자식이 1개 밖에 없으면, 현재 노드의 자식을 삭제할 경우 그 현재 노드가 리프노드가 됨 if(graph[node].size() == 1) { leafCnt++; } continue; } find_leaf(graph[node][i]); } } int main(void) { // freopen("B1068_input.txt", "r", stdin); cin >> N; for(int to = 0; to < N; to++) { int from; cin >> from; if(from == -1) { rootNode = to; continue; } else { graph[from].push_back(to); } } cin >> delNode; if(delNode == rootNode) { cout << "0"; } else { find_leaf(rootNode); cout << leafCnt; } return 0; } | cs |
'Baekjoon > DFS' 카테고리의 다른 글
[백준 17779] 게리맨더링2 (DFS) (C/C+) (★) (0) | 2021.01.17 |
---|---|
[백준 1325] 효율적인 해킹 (DFS) (C/C++) (★) (0) | 2020.05.24 |
[백준 9466] 텀 프로젝트 (DFS) (C/C++) (★★★) (0) | 2020.01.26 |
[백준 2667] 단지번호 붙이기 (DFS) (C/C++) (0) | 2020.01.26 |
[백준 10451] 순열 사이클 (DFS) (C/C++) (0) | 2020.01.26 |