Pod Placement Planner

medium backtracking bitmask dp 1000 ms · 256 MB

Pod Placement Planner

Your team is shrinking a staging cluster and wants to know how few machines it really needs. All nodes in the cluster have the same capacity: C millicores of CPU and M MiB of memory.

There are n pods to schedule. Pod i requests c_i millicores and m_i MiB. Every pod must be placed on exactly one node, and on each node the total CPU requested by its pods must not exceed C, and the total memory must not exceed M.

Some pods are replicas of the same service and carry an anti-affinity rule: for each of the p listed pairs (a, b), pods a and b must run on different nodes.

Find the minimum number of nodes needed to place all pods, together with one placement that achieves it. If some pod cannot fit on a node even when it runs alone, print -1.

Input

The first line contains four integers n, p, C and M (1 ≤ n ≤ 15, 0 ≤ p ≤ n·(n−1)/2, 1 ≤ C, M ≤ 10^6).

Each of the next n lines contains two integers c_i and m_i (1 ≤ c_i, m_i ≤ 10^6): the CPU and memory requests of pod i.

Each of the next p lines contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b): pods a and b must not share a node. No unordered pair is listed twice.

Output

If some pod has c_i > C or m_i > M, print a single line -1.

Otherwise print the minimum number of nodes k on the first line. On the second line print n integers x_1, x_2, …, x_n (1 ≤ x_i ≤ k), where x_i is the node that pod i is placed on. The placement must respect both capacities on every node and every anti-affinity rule. If several placements use k nodes, print any of them.

Notes

In the first example, pods 1 and 2 are anti-affine, so at least two nodes are needed. Two nodes suffice: pods 1 and 3 use 900 millicores and 800 MiB, pods 2 and 4 use 900 millicores and exactly 1000 MiB. Placing pods 3 and 4 together would not work, since they need 1100 MiB.

In the second example, pod 3 asks for more memory than a single node has, so no placement exists.

Any correct answer is accepted.

Example 1

Input

4 1 1000 1000
500 300
400 400
400 500
500 600
1 2

Output

2
1 2 1 2

Example 2

Input

3 0 2000 4096
500 1024
1500 2048
100 5000

Output

-1

Hints · reveal one at a time if you're stuck

Hint 1

Start with the impossible case: a pod larger than one node in either resource gives -1. Otherwise, with n at most 15, what does the placement look like as a grouping of pods, and why is a search over groupings feasible while assigning each pod to any node is not?

Hint 2

Nodes are identical, so only the grouping matters. Place pods one at a time, letting each pod either join an already opened node or open the next new one, so every grouping is generated once rather than once per relabelling.

Hint 3

Prune the search with the best answer found so far, and stop early when a lower bound from the total CPU or total memory is reached. Placing the largest pods first makes the bad branches die quickly. Check both resources and the anti-affinity pairs, which must be treated as symmetric.

report an issue Prefer your own editor? sooks prep 46 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.