2-SAT asks how to pick a value for each of the boolean variables x1,x2,…,xN so that a 2-CNF formula becomes true.
A 2-CNF formula looks like (x∨y)∧(¬y∨z)∧(x∨¬z)∧(z∨y). Each parenthesized part 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 N, the number of clauses M, and the formula f. Write a program that decides whether f can be made true.
For example, take N=3, M=4, and f=(¬x1∨x2)∧(¬x2∨x3)∧(x1∨x3)∧(x3∨x2). Setting x1 to false, x2 to false, and x3 to 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 holds the number of variables N (1≤N≤10000) and the number of clauses M (1≤M≤100000). Each of the next M lines holds one clause as two integers i and j (1≤∣i∣,∣j∣≤N). A positive i means xi and a negative i means ¬x−i. The same rule applies to j.
Print 1 on the first line if f can be made true, and 0 otherwise.