This page is still under construction.

Parts of this page are still being built. What you see may change.

2-SAT Satisfiability

Time limit1sMemory limit256 MB

Summary
Decide whether N boolean variables admit values that satisfy all M two-literal clauses.
Level

Medium7 of 10

Topics
Graph, DFS
Solved
No attempts yet

Problem

2-SAT asks how to pick a value for each of the boolean variables x1,x2,…,xNx_1, x_2, \ldots, x_N so that a 2-CNF formula becomes 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 joins two variables with ∨\lor. Here ∨\lor is OR, ∧\land is AND, and ¬\lnot is NOT.

You are 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. For 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 holds the number of variables NN (1≤N≤100001 \le N \le 10000) and the number of clauses MM (1≤M≤1000001 \le M \le 100000). Each of the next MM lines holds one clause as two integers ii and jj (1≤∣i∣,∣j∣≤N1 \le |i|, |j| \le N). A positive ii means xix_i and a negative ii means ¬x−i\lnot x_{-i}. The same rule applies to jj.

Output

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

Examples2

  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