2-SAT asks whether the boolean variables x1,x2,…,xN can be assigned values that make a given 2-CNF formula true.
A 2-CNF formula looks like (x∨y)∧(¬y∨z)∧(x∨¬z)∧(z∨y). Each parenthesized part is a clause. A clause joins two literals with ∨, and a literal is a variable or the negation of a variable. Here ∨ is OR, ∧ is AND, and ¬ is NOT.
Given the number of variables N, the number of clauses M, and the formula f, decide whether f can be made true, and give the values of the variables when it can.
For N=3, M=4, and f=(¬x1∨x2)∧(¬x2∨x3)∧(x1∨x3)∧(x3∨x2), setting x1 false, x2 false, and x3 true makes f true. For N=1, M=2, and f=(x1∨x1)∧(¬x1∨¬x1), no value of x1 makes f true.
The first line contains the number of variables N (1≤N≤20) and the number of clauses M (1≤M≤100).
Each of the next M lines holds one clause as two integers i and j (1≤∣i∣,∣j∣≤N). A positive i means the literal xi, and a negative i means the literal ¬x−i. The same variable may appear twice in one clause, and the same clause may be given more than once.
Print 1 on the first line if f can be made true, and 0 if it cannot.
If it can, print the values of x1 through xN in order on the second line, separated by single spaces. Write 1 for true and 0 for false.
When several assignments make f true, print only the one whose sequence (x1,x2,…,xN) is lexicographically smallest. That is, walk the variables in order: keeping the values already fixed, set the current variable to 0 if the remaining variables can still make f true, and set it to 1 only when they cannot.