RiseOJ는 solved.ac와 제휴 관계가 없습니다. 티어 아이콘 © solved.ac. solved.ac
포럼
USACO0554

Subtree Activation

Platinum I 플래티넘 I
난이도
2s
시간 제한
256MB
메모리 제한
0
맞았습니다!!
0
제출 수
0.0%
정답률
레이팅

의견: 0

설명

For the New Year celebration, Bessie and her friends have constructed a giant
tree with many glowing ornaments. Bessie has the ability to turn the ornaments
on and off through remote control. Before the sun rises, she wants to toggle
some of the ornaments in some order (possibly toggling an ornament more than
once) such that the tree starts and ends with no ornaments on. Bessie thinks the
tree looks cool if the set of activated ornaments is exactly a subtree rooted at
some vertex. She wants the order of ornaments she toggles to satisfy the
property that, for every subtree, at some point in time it was exactly the set
of all turned on ornaments. Additionally, it takes energy to switch on and off
ornaments, and Bessie does not want to waste energy, so she wants to find the
minimum number of toggles she can perform.

Formally, you have a tree with vertices labeled \(1\dots N\)
(\(2\le N\le 2\cdot 10^5\)) rooted at \(1\). Each vertex is initially inactive. In
one operation, you can toggle the state of a single vertex from inactive to
active or vice versa. Output the minimum possible length of a sequence of
operations satisfying both of the following conditions:

  • Define the subtree rooted at a vertex \(r\) to consist of all vertices \(v\) such that \(r\) lies on the path from \(1\) to \(v\) inclusive. For every one of the \(N\) subtrees of the tree, there is a moment in time when the set of active vertices is precisely those in that subtree.
  • Every vertex is inactive after the entire sequence of operations.

Problem credits: Benjamin Qi

제약

SCORING

  • Inputs 2-3: \(N \le 8\)
  • Inputs 4-9: \(N \le 40\)
  • Inputs 10-15: \(N \le 5000\)
  • Inputs 16-21: No additional constraints.

Problem credits: Benjamin Qi

입력 형식

The first line contains \(N\).

The second line contains \(p_2 \dots p_N\) (\(1\le p_i), where \(p_i\) denotes the
parent of vertex \(i\) in the tree.

출력 형식

Output the minimum possible length.

예제 1
입력
3
1 1
출력
6
설명

There are three subtrees, corresponding to \(\{1,2,3\}\), \(\{2\}\), and \(\{3\}\).
Here is one sequence of operations of the minimum possible length:

activate 2
(activated vertices form the subtree rooted at 2)
activate 1
activate 3
(activated vertices form the subtree rooted at 1)
deactivate 1
deactivate 2
(activated vertices form the subtree rooted at 3)
deactivate 3
문제 정보

riseoj 작성

출처 올림피아드 > USACO > 2022-2023 > January > Platinum

태그

평가 및 의견

Subtree Activation

개요
출제자 난이도 Platinum I 플래티넘 I 의견 0 / 50 공개 집계 (커뮤니티 난이도, 주요 주제, 품질)는 의견이 충분히 모이면 공개됩니다.

Log in to rate problems.

개별 의견

아직 의견이 없습니다. 자격이 된다면 위 양식에서 가장 먼저 평가해 보세요.

풀이 제출

Subtree Activation

게스트로 둘러보고 있습니다. 로그인하면 풀이를 제출하고 진행 상황을 확인할 수 있습니다. 로그인하고 제출하기
공개
C++20 Tab 들여쓰기 · Ctrl+/ 주석 토글 · Enter 자동 들여쓰기
1 1 1 0 공백: 4 · UTF-8