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

Fireworks

설명

A fireworks display is built as a rooted tree. Node \(1\) is the root (the ignition switch). The tree has \(N\) internal nodes (fuse-connectors, numbered \(1\) to \(N\)) and \(M\) leaf nodes (the explosives, numbered \(N+1\) to \(N+M\)). Every node except the root is connected to its parent by exactly one fuse.

When the switch is lit the fire travels along the fuses at a constant speed. For all explosives to detonate at the same instant, the total fuse length from the root to every leaf must be identical.

You may change the length of any fuse; changing a fuse of length \(\ell\) to a new non-negative integer length \(\ell'\) costs \(|\ell' - \ell|\). Find the minimum total cost to make the root-to-leaf fuse-length equal for every explosive.

제약

\(1 \le N, M\) and \(N + M \le 300\,000\); \(1 \le C_i \le 10^9\).

  • Subtask 1 (7 points): \(N = 1\).
  • Subtask 2 (19 points): \(N + M \le 300\).
  • Subtask 3 (29 points): \(N + M \le 5\,000\).
  • Subtask 4 (45 points): \(N + M \le 300\,000\).
입력 형식

The first line contains two integers \(N\) and \(M\) — the number of internal nodes and the number of explosives.

Each of the next \(N + M - 1\) lines describes node \(i\) (for \(i = 2\) to \(N + M\)) with two integers \(P_i\) and \(C_i\): \(P_i\) is the parent of node \(i\) \((1 \le P_i < i)\) and \(C_i\) is the length of the fuse joining node \(i\) to its parent \((1 \le C_i \le 10^9)\).

출력 형식

Output a single integer: the minimum total cost so that the root-to-leaf fuse length is the same for every explosive.

서브태스크
서브태스크점수설명

Subtask 1

7점

None

Subtask 2

19점

None

Subtask 3

29점

None

Subtask 4

45점

None

문제 정보

rip 작성

출처 APIO 2016

평가 및 의견

Fireworks

개요
출제자 난이도 Unrated 레이팅 미적용 의견 0 / 50 공개 집계 (커뮤니티 난이도, 주요 주제, 품질)는 의견이 충분히 모이면 공개됩니다.

Log in to rate problems.

개별 의견

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

풀이 제출

Fireworks

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