루미 알고리즘

#1393다이아Kingdom Division

PO 2016 훈련 캠프코딩출제 Johan Sannemo영어 원문이 문제가 어렵다면 — 배우는 순서 보기

영어 원문입니다. 원문의 수식·표를 글자로 옮겨 모양이 다를 수 있습니다. 정확한 내용은 원문 페이지에서 확인하세요. 원문 페이지 열기 (새 창)

Problem

In a far away kingdom, there are N cities numbered between 0 and N - 1. The cities are connected by N - 1 two-way roads. Each road connects exactly two cities, such that there is a unique path between any pair of cities. Each city i has some value C[i] (possibly negative).

Unfortunately, the old king of the kingdom died, without appointing a successor. Thus a civil war broke out in the kingdom, between the P lords (also numbered between 0 and P - 1) who wish to gain control of the kongdom.

After a long and terrible war, the lords realized none of them could beat all the other lords in the war. They agreed to a truce, and decided to divide the kingdom into P parts, one part per lord. The parts must be so that if two cities a and b lie in the same part, all cities in the unique path between them must also lie in that part. Since no lord wants to be left out, each one must be given at least one city.

Since no lord wants to get less than another, the value of each part must be the same. The value of a part is the sum of the values of all cities in that part.

Example

Let the kingdom have N = 5 cities, connected by roads as in the figure below:

(그림은 원문 저장소에서 볼 수 있습니다)

Figure 1: Illustration of the example

Each city has its value given within parenthesis after the city number.

In total, we wish to divide the kingdom into P = 3 parts. Then, a possible subdivision would be (0, 2), (3) and (1, 4). The values of each part would then be -4 + 3 = -1, -1 and 3 - 4 = -1, so all parts in this subdivision would have the same value.

Note that (0, 1), (3), (2, 4) would not be an acceptable division even though all parts have the same value. This is because the path between cities 0 and 1 is 1, 4, 2, 0, but cities 4 and 2 belong to another part.

Task

Your task is to compute a subdivision of the kingdom as described in the statement. You will implement the function division(N, P, C, F, T).

• division(N, P, C, F, T) - this function will be called exactly once by the judge.

  • N: the number of cities in the kingdom.
  • P: the number of parts we wish to divide the kingdom into.
  • C: an array with N elements. C[i] (0 ≤ i < N) contains the value of city i.
  • F: an array with N - 1 elements. F[i] (0 ≤ i < N - 1) contains one of the cities that the i:th road connects.
  • T: an array with N - 1 elements. T[i] (0 ≤ i < N - 1) contains the other city that the i:th road connects.
  • It is always possible to travel between any pair of cities using the roads.
  • |C[i]| < 10^9
  • The function should return 1 if it is possible to find such a subdivision, and 0 if it is impossible.

Additionally, you should make a single call to function parts(S) to construct your subdivision.

• parts(S) - this function should be called a single time.

  • S: an array with N elements. S[i] (0 ≤ i < N) should contain the number of the lord who will get city i.
  • The function has no return value.

A code skeleton containing the function to be implemented, together with a sample grader, can be found at http://progolymp.se/uploads/kattis-attachments/kingdom.zip.

Subtasks

The problem consists of a number of subtasks. Each subtask gives some amount of points, and to pass the subtask you must pass all the test cases in the subtask.

If your partition is incorrect, but you correctly determine whether a partition exists, your solution will get 50% of the score.

SubtaskPointsLimits
18P = 2 ≤ N ≤ 1000
2101 ≤ P ≤ N ≤ 1000, C[i] > 0
3161 ≤ P ≤ N ≤ 1000, only cities i and i + 1 have roads (for 0 ≤ i < N - 1).
4181 ≤ P ≤ N ≤ 1000
5181 ≤ P ≤ N ≤ 100000, C[i] > 0
6301 ≤ P ≤ N ≤ 100000

Input format

The sample judge reads input in the following format:

  • line 1: N P
  • line 2: C[0] C[1] .. C[N - 1]
  • line 3: F[0] F[1] .. F[N - 2]
  • line 4: T[0] T[1] .. T[N - 2]

Output format

The sample judge will first write a single line with the return value of division(N, P, C, F, T). The next line will contain N integers containing the input to the parts(R) call.

예제

예제 1

입력
5 3
-4 3 3 -1 -4
0 1 2 3
2 4 4 4
출력
1
0 1 0 2 1

출처 표기

출처: Programmeringsolympiaden 2016년 훈련 캠프 · 원문 페이지 · 이용 조건 CC BY-SA · LaTeX 원문을 화면용 글로 옮겼습니다(수식·표의 모양이 다를 수 있습니다). 내용은 고치지 않았습니다.

크리에이티브 커먼즈 저작자표시-동일조건변경허락.

문제 출처: 한국정보올림피아드(KOI) (CC BY-NC-SA 4.0) · International Olympiad in Informatics (IOI) (CC BY) · Centre for Education in Mathematics and Computing (CEMC), University of Waterloo (CC BY-NC 4.0) · Programmeringsolympiaden (CC BY-SA) · CSES Problem Set (Antti Laaksonen) (CC BY-NC-SA 4.0). 루미 알고리즘은 위 단체들과 관계없는 별도의 서비스이며, 자료는 원문 그대로 싣습니다. 문제마다 출처와 이용 조건을 따로 표기합니다.