This page is still under construction.

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

2-SAT smallest assignment

Time limit1sMemory limit256 MB

Summary
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 x1,x2,…,xNx_1, x_2, \dots, x_N so that a given 2-CNF formula is 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 part inside parentheses 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. Decide whether some assignment makes ff true, and when one exists, find the assignment that comes first in lexicographic order.

An assignment is the sequence x1,x2,…,xNx_1, x_2, \dots, x_N in which each xix_i is 0 (false) or 1 (true). To compare two assignments, read them from x1x_1 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 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 contains one clause.

A clause is two nonzero integers ii and jj (1≤∣i∣,∣j∣≤N1 \le |i|, |j| \le N). A positive number means the variable with that index and a negative number means the negation of that variable, so ii stands for xix_i when it is positive and for ¬x−i\lnot x_{-i} when it is negative. The same rule applies to jj. 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 ff 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 x1x_1 to xNx_N, separated by spaces. Print 1 for true and 0 for false. If it cannot, print only the first line.

Examples2

  1. Example 1

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

    Input
    1 2
    1 1
    -1 -1
    
    Expected output
    0