2-SAT smallest assignment
Time limit1sMemory limit256 MB
Decide whether a 2-CNF formula over up to 10000 variables is satisfiable and output the lexicographically smallest satisfying assignment.
- Level
Hard8 of 10
- Topics
- Graph, DFS, Topological sort, Greedy
- Solved
- No attempts yet
Problem
2-SAT asks you to choose values for the boolean variables so that a given 2-CNF formula is true.
A 2-CNF formula looks like . Each part inside parentheses is a clause, and a clause joins two variables with . Here is OR, is AND, and is NOT.
You are given the number of variables , the number of clauses , and the formula . Decide whether some assignment makes true, and when one exists, find the assignment that comes first in lexicographic order.
An assignment is the sequence in which each is 0 (false) or 1 (true). To compare two assignments, read them from and find the first position where they differ. The one holding 0 at that position comes first.
Input
The first line contains the number of variables () and the number of clauses (). Each of the next lines contains one clause.
A clause is two nonzero integers and (). A positive number means the variable with that index and a negative number means the negation of that variable, so stands for when it is positive and for when it is negative. The same rule applies to . One clause can hold the same variable twice, and the same clause can be given more than once.
Output
On the first line print 1 if can be made true, and 0 if it cannot.
If it can, print on the second line the assignment that comes first in lexicographic order, from to , separated by spaces. Print 1 for true and 0 for false. If it cannot, print only the first line.