Kingdom Division

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:

\includegraphics[width=0.3\textwidth ]{sample.png}
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 \le i < N$) contains the value of city $i$.

    • F: an array with $N - 1$ elements. F[i] ($0 \le i < N - 1$) contains one of the cities that the $i$:th road connects.

    • T: an array with $N - 1$ elements. T[i] ($0 \le 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 \le 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.

Subtask

Points

Limits

1

8

$P = 2 \le N \le 1\, 000$

2

10

$1 \le P \le N \le 1\, 000$, $C[i] > 0$

3

16

$1 \le P \le N \le 1\, 000$, only cities $i$ and $i + 1$ have roads (for $0 \le i < N - 1$).

4

18

$1 \le P \le N \le 1\, 000$

5

18

$1 \le P \le N \le 100\, 000$, $C[i] > 0$

6

30

$1 \le P \le N \le 100\, 000$

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.

Sample Input 1 Sample Output 1
5 3
-4 3 3 -1 -4
0 1 2 3
2 4 4 4
1
0 1 0 2 1