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

Potion Farming

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

의견: 0

설명

You are playing your favorite mobile game and you are trying to farm potions so
that you may have a chance at defeating the legendary cow boss. The game map is a
series of \(N\) \((2 \leq N \leq 10^5)\) rooms labeled \(1\dots N\) connected by
\(N-1\) edges that form a tree.

You can explore the map by making a series of "traversals". A traversal is a
simple path from room \(1\) to any other room in the tree. Once you finish one
traversal, you can start another traversal from room \(1\). The map is complete
once every one of its rooms is visited by at least one traversal. Your main goal
is to complete the map in the minimum number of traversals.

Your secondary goal is to farm as many potions as possible. Before a traversal
begins, a potion will spawn at some room in the map. You can pick up the potion
by visiting the room that the potion spawned at in the current traversal. If you
do not pick up the potion, then it will disappear once the current traversal
ends, so you cannot pick it up in future traversals.

As you are a smart programmer, after looking at the game files, you were able to
figure out where the potions will appear before your next \(N\) traversals. If you
complete the map in the minimum number of traversals, what is the maximum amount
of potions that you can farm from the map?

Problem credits: Suhas Nagar

제약

SCORING

  • Inputs 2-7: \(N\le 1000\)
  • Inputs 8-15: No additional constraints.

Problem credits: Suhas Nagar

입력 형식

The first line of the input contains an integer \(N\), denoting the number of
rooms in the map.

Then follow \(N\) space-separated integers \(p_1 \: p_2 \: \ldots \: p_N\) where
\(1 \leq p_i \leq N\), where \(p_i\) is the room that a potion will appear at before
the \(i\)th traversal.

Finally, \(N-1\) lines follow with two space-separated integers \(a \: b\)
\((1 \leq a, b \leq N)\) representing an edge between rooms \(a\) and \(b\). It is
guaranteed that these edges form a tree.

출력 형식

Output one line containing a single integer, the maximum amount of potions that
you can farm from the map in the minimum number of traversals.

예제 1
입력
5
5 4 3 2 1
1 2
1 3
3 4
3 5
출력
2
설명

In this case, the minimum number of traversals required to complete the map is
\(3\).

One optimal plan that picks up two potions in three traversals is as follows:

  • Traversal 1: \(1 \rightarrow 3 \rightarrow 5\) (Pick up potion at 5)
  • Traversal 2: \(1 \rightarrow 3 \rightarrow 4\) (Pick up potion at 4)
  • Traversal 3: \(1 \rightarrow 2\) (Forced to complete the map and ignore potion at 3)

Alternatively, we could have also planned our traversals as follows:

  • Traversal 1: \(1 \rightarrow 2\) (no potions)
  • Traversal 2: \(1 \rightarrow 3 \rightarrow 4\) (Pick up potion at 4)
  • Traversal 3: \(1 \rightarrow 3 \rightarrow 5\) (Pick up potion at 3)
문제 정보

riseoj 작성

출처 올림피아드 > USACO > 2023-2024 > January > Silver

태그

평가 및 의견

Potion Farming

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

Log in to rate problems.

개별 의견

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

풀이 제출

Potion Farming

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