Modified SAT
Time limit2sMemory limit512 MB
Given a CNF formula whose clauses each have at most 3 literals, decide whether there is an assignment with exactly 1 or exactly 3 true literals per clause, and print the lexicographically largest such assignment.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy, Implementation
- Solved
- No attempts yet
Problem
The Boolean satisfiability problem (SAT) asks, given a Boolean formula in conjunctive normal form, whether there is an assignment of true or false to its variables that makes the whole formula true.
In conjunctive normal form, the formula has a specific shape. The only logical operations used are AND, OR, and negation, written , , and . A formula is the AND of several parts called clauses, . So a formula has the form
Each clause also has a specific shape. A clause is the OR of literals, which are variables or negations of variables, enclosed in parentheses. So is a valid clause, while is not, because it uses AND. A complete example formula is
A variation of SAT is k-SAT, where each clause has at most literals. The formula above is a 3-SAT instance but not a 2-SAT instance. In all these problems, for a formula to be true every clause must be true, so at least one literal (of the form or ) in each clause must be true.
An assignment is a way of setting the variables to true or false. This problem concerns a variation of 3-SAT in which a valid assignment must have exactly 1 or exactly 3 true literals in each clause. Given a formula, your task is to decide whether a valid assignment exists under this extra restriction. If a valid assignment exists, you must print the lexicographically largest one. Lexicographic order is defined as follows: given two different assignments, compare them at the lowest-indexed variable where they differ; the larger assignment is the one that sets that variable to true.
Input
The first line contains two integers and (), the number of clauses and variables. Then lines follow, each describing one clause (see the example for the format). Consecutive clauses are separated by the string “ and”. Each clause contains at most 3 literals. Variables are written as “x” followed by a number between 1 and . There are no two consecutive spaces, and no space at the end of a line.
The first example describes the formula above.
Output
Print one line with characters for the lexicographically largest valid assignment, or impossible if no valid assignment exists. The -th character is T if variable is true in the assignment and F otherwise.