MarriageProblemRevised
SRM 356 · 2007-07-02 · by Pawa
Problem Statement
Your task is to group these people into the minimum possible number of marriages. Each person must be a member of exactly one marriage, and each marriage must contain only willing members. Return the number of marriages, or -1 if this is not possible.
Constraints
- preferences will contain between 1 and 12 elements, inclusive.
- The length of each element of preferences will be betwen 1 and 12, inclusive.
- All elements of preferences will be of the same length.
- Each element of preferences will contain only '0' (zeroes) and '1' (ones).
{"111"}
Returns: 1
Here, we have one man and three women, and everybody is willing to be in the same marriage. Therefore, we only need one marriage.
{"100", "010", "001"}
Returns: 3
{"00", "00"}
Returns: -1
Nobody is willing to be in the same marriage as anybody else, so there cannot be any marriages in this case.
{"0001", "0001", "0001", "1111"}
Returns: 2
{"11101011","00011110","11100100","01010000","01000010","10100011","01110110","10111111"}
Returns: 3
Submissions are judged against all 96 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class MarriageProblemRevised with a public method int neededMarriages(vector<string> preferences) · 96 test cases · 2 s / 256 MB per case