문제 설명
트리에서 리프 노드란, 자식의 개수가 0인 노드를 말한다.
트리가 주어졌을 때, 노드 하나를 지울 것이다. 그 때, 남은 트리에서 리프 노드의 개수를 구하는 프로그램을 작성하시오. 노드를 지우면 그 노드와 노드의 모든 자손이 트리에서 제거된다.
예를 들어, 다음과 같은 트리가 있다고 하자.

현재 리프 노드의 개수는 3개이다. (초록색 색칠된 노드) 이때, 1번을 지우면, 다음과 같이 변한다. 검정색으로 색칠된 노드가 트리에서 제거된 노드이다.

이제 리프 노드의 개수는 1개이다.
https://www.acmicpc.net/problem/1068
제한 사항


풀이
문제를 요약하면 트리의 구조가 주어지고 특정 노드를 하나 삭제했을 때 리프노드의 개수를 구하면 된다.
해당 문제는 간단하지만 예외처리가 조금 필요하다.
우선 트리를 표현하는 방법을 생각해 보면 부모 노드가 주어지기 때문에 이를 이용하여 자식 노드를 관리할 수 있다.
int root = -1;
for (int i = 0; i < N; i++)
{
int p;
cin >> p;
if (p == -1)
{
root = i;
continue;
}
children[p].push_back(i);
}
이때, 루트 노드가 항상 0이 아니라는 점을 유의해야 한다.
그리고 루트 노드는 항상 1개만 주어지기 때문에 -1이 입력되면 루트로 설정한다.
삭제할 노드를 입력받고 해당 노드의 자식들을 모두 지우면 전처리는 끝난다.
cin >> D;
children[D].clear();
if (D == root)
{
cout << 0;
return 0;
}
이때, 삭제하는 노드가 루트라면 0을 출력하고 종료한다.
이제 루트 노드에서 순회하며 리프 노드인지 확인하면 된다.
만약 자식 노드가 1개이고 자식 노드가 삭제된 노드라면 부모 노드가 리프노드가 된다.
삭제하는 노드가 1개로 한정되어 있기 때문에 이 논리는 참이 된다.
while (!q.empty())
{
auto current = q.front();
q.pop();
if (current == D) continue;
if (visited[current]) continue;
if (children[current].empty())
{
ans++;
continue;
}
visited[current] = true;
if (children[current].size() == 1 && children[current][0] == D)
{
ans++;
continue;
}
for (auto child : children[current])
{
q.push(child);
}
}
전체 코드
#include <bits/stdc++.h>
#include <unordered_set>
using namespace std;
#define INPUT_OPTIMIZE cin.tie(NULL); cout.tie(NULL); ios::sync_with_stdio(false);
#define INF 2e9
using namespace std;
int N, D;
vector<vector<int>> children;
vector<bool> visited;
int ans = 0;
int main()
{
INPUT_OPTIMIZE;
cin >> N;
children.resize(N, vector<int>());
visited.resize(N, false);
int root = -1;
for (int i = 0; i < N; i++)
{
int p;
cin >> p;
if (p == -1)
{
root = i;
continue;
}
children[p].push_back(i);
}
cin >> D;
children[D].clear();
if (D == root)
{
cout << 0;
return 0;
}
queue<int> q;
q.push(root);
while (!q.empty())
{
auto current = q.front();
q.pop();
if (current == D) continue;
if (visited[current]) continue;
if (children[current].empty())
{
ans++;
continue;
}
visited[current] = true;
if (children[current].size() == 1 && children[current][0] == D)
{
ans++;
continue;
}
for (auto child : children[current])
{
q.push(child);
}
}
cout << ans;
return 0;
}