MIKE3 - Editorial



Author: Constantine Sokol
Tester: Mahbubul Hasan
Editorialist: Jingbo Shang






Given N objects and M weighted subsets of them, find a combination of subsets, such that all objects are covered at most once and the sum of the weights are maximized.

Some test cases are added in the practice room.


Although N is large, M is quite small such that 2^M is tolerant. Therefore, we first need to construct the conflicts between different subsets. Two subsets are called conflict if and only if they shares a common object. To get this conflicts table, on each object, we can restore the IDs of subsets who contain the object. And then, mark they are conflicted with each other. This step takes O(N * M^2). Moreover, we can reduce the time complexity using the binary expression as following.

conflict[li] = 0;

[/li] For i = 1 to N do
int mask = 0;
For ID in IDs[i] do
mask |= 1 << ID;
For ID in IDs[i] do
conflict[ID] |= mask;

where, conflict[i] >> j & 1 stands for whether the i-th and j-th subsets are conflicted with each other. This code needs only O(NM) time.

And then, a simple DFS search can be applied to find the maximum weighted valid choice, like following:

int DFS(int i, int mask, int sum)
    if (i == M) {
        return sum;
    int ret = DFS(i + 1, mask, sum); // don't select the i-th subset
    if (mask >> i & 1) {
        // conflicted, can't be chosen.
    } else {
        // try to select the i-th subset
        ret = max( ret, DFS(i + 1, mask | conflict[i], sum + weight[i]);
    return ret;

This procedure takes O(2^M) time. Therefore, the overall time complexity is O(NM+2^M).


Author’s solution can be found here.
Tester’s solution can be found here.


I formed a graph in O(N*M2) and then considering one offer as accepted found the maximum clique in its subgraph in O(M4). I have tried to explain my approach here


Shouldn’t the overall complexity be O(NM^2 + 2^M) ?

O(N * M^2) would lead to TLE, for N = 20.000 and M = 20…

I did the same, instead of finding maximum clique, I found maximum independent set.


by NM^2 i mean N*(M^2)

1 Like

@betlista No, it won’t.

1 Like

sorry, I missed 2^M and M^2 difference O:-) …

How can you say that your complexity is O(M^4)?.

for(every node in graph) //O(M*O(clique()))
  clique()      //O(M<sup>3</sup>)
  while( a node exists in sub-graph)
     for(every node in subgraph)
       for( evry node its connected to)

Hence overall complexity is O(M4)

As the problem is NP complete and you are giving a polynomial time solution of this problem. You have managed to prove P = NP :stuck_out_tongue:


No, the hack here was that the original problem required all the 2^M combinations. But for this problem we will always form a subgraph such that we can solve it in M^4.

cant we use dp here ?
If we able to encode a set into binary number(hence decimal number ) .

1 Like

can this problem solved by using sorting with respect to size of each subset ? pls reply
my code is

Using the binary representation, we can do it in O(NM) rather than O(NM^2).

Oh yes, I got it !!!

I highly doubt your O(M^4) for a NP complete problem :slight_smile:
Created a dependency graph for all the offers. This graph has atmost 20 nodes. then calculated the maximum independent set using max cliques. Complexity is around NM^2 + 1.3^M

On getting AC even I thought of weak test cases but couldn’t find a test case in which this approach fails. It would be great if you could tell one :slight_smile:
The main reason behind it getting AC was the property of subgraph, it contains a permanent node such that every other node in the subgraph is connected to it. In the removal process I never touch this permanent node. So when I remove a node from subgraph it doesn’t affect the graph in the same way as it does for the Clique Problem

yes , we can solve it using it dp . We can solve it by initially getting the maximum number of non overalapping sets . Then we can use binary number to generate sets like 1100 means i took offer 1,offer 2 and rejected offer 3 and 4 . I solved it this way because i dont know much about graph

@shangjingbo :diamonds::diamonds:
can you please tell me how DFS is implemented here. Can you provide any good link to read this.
How to see nodes and edge’s.
Thank you.
PS : i followed author’s solution.