Knapsack Problem
Time limit2sMemory limit1024 MB
Given item weights and capacity c, decide whether the sets of items with total weight at most c form a matroid, printing a counterexample if not.
- Level
Medium5 of 10
- Topics
- Math, Greedy, Brute force
- Solved
- No attempts yet
Problem
The knapsack problem is stated as follows. There are items, and the -th item has weight and value . The task is to choose a set of items with maximum total value such that their total weight does not exceed , the capacity of the knapsack. The knapsack problem is NP-hard, but it can be solved with dynamic programming in time proportional to the total weight of the items.
However, the knapsack problem can be solved greedily in some cases. One such case is when an instance of the knapsack problem forms a matroid.
A matroid is a pair , where is a finite set and is a family of subsets of . The elements of are called independent. The family must satisfy the following axioms.
- ;
- If and , then ;
- If and , then there exists such that .
Here is the number of items in the set .
An example of a matroid is the set of edges of an undirected graph, where an edge set is independent if it is acyclic.
Let the items have weights , and take these items as . A set of items is called independent if . Determine whether this construction forms a matroid for the given and .
Input
The first line contains an integer (). The second line contains integers (). The third line contains an integer ().
Output
Print YES if the set of solutions to the knapsack problem forms a matroid. Otherwise, print NO.
In the second case, print on the second line one integer, the number of the axiom that is violated. If axiom 2 or axiom 3 is violated, print on the next two lines the sets and for which the axiom does not hold. Each set is described by the number of items in it, followed by the numbers of those items.