Under construction

Still being built. Things may change

2-SAT Satisfiability

Interview

Time limit1sMemory limit256 MB

Summary
Decide whether N boolean variables admit an assignment satisfying all M two-literal clauses.
Level

Easy3 of 10

Topics
Brute force, Bit manipulation
Solved
No attempts yet

Problem

2-SAT asks whether boolean variables x1,x2,…,xNx_1, x_2, \ldots, x_N can be assigned values that make a 2-CNF formula true.

A 2-CNF formula looks like (x∨y)∧(¬y∨z)∧(x∨¬z)∧(z∨y)(x \lor y) \land (\lnot y \lor z) \land (x \lor \lnot z) \land (z \lor y). Each parenthesized part is a clause, and a clause is two variables joined by ∨\lor. Here ∨\lor is OR, ∧\land is AND, and ¬\lnot is NOT.

Given the number of variables NN, the number of clauses MM, and the formula ff, write a program that decides whether ff can be made true.

For example, take N=3N = 3, M=4M = 4, and f=(¬x1∨x2)∧(¬x2∨x3)∧(x1∨x3)∧(x3∨x2)f = (\lnot x_1 \lor x_2) \land (\lnot x_2 \lor x_3) \land (x_1 \lor x_3) \land (x_3 \lor x_2). Setting x1x_1 to false, x2x_2 to false, and x3x_3 to true makes ff true. With N=1N = 1, M=2M = 2, and f=(x1∨x1)∧(¬x1∨¬x1)f = (x_1 \lor x_1) \land (\lnot x_1 \lor \lnot x_1), no value of x1x_1 makes ff true.

Input

The first line has the number of variables NN (1≤N≤201 \le N \le 20) and the number of clauses MM (1≤M≤1001 \le M \le 100). Each of the next MM lines has one clause, given as two integers ii and jj (1≤∣i∣,∣j∣≤N1 \le |i|, |j| \le N). A positive integer means xix_i or xjx_j, and a negative integer means ¬x−i\lnot x_{-i} or ¬x−j\lnot x_{-j}.

Output

Print 1 on the first line if ff can be made true, and 0 otherwise.

Examples4

  1. Example 1

    Input
    3 4
    -1 2
    -2 3
    1 3
    3 2
    
    Expected output
    1
    
  2. Example 2

    Input
    1 2
    1 1
    -1 -1
    
    Expected output
    0
    
  3. Example 3

    Input
    1 1
    1 1
    
    Expected output
    1
    
  4. Example 4

    Input
    1 1
    -1 1
    
    Expected output
    1