Under construction

Still being built. Things may change

Modified SAT

Time limit2sMemory limit512 MB

Summary
Given a CNF formula whose clauses each have at most 3 literals, decide whether there is an assignment with exactly 1 or exactly 3 true literals per clause, and print the lexicographically largest such assignment.
Level

Hard8 of 10

Topics
Dynamic programming, Greedy, Implementation
Solved
No attempts yet

Problem

The Boolean satisfiability problem (SAT) asks, given a Boolean formula in conjunctive normal form, whether there is an assignment of true or false to its variables that makes the whole formula true.

In conjunctive normal form, the formula has a specific shape. The only logical operations used are AND, OR, and negation, written ∧\wedge, ∨\vee, and ¬\neg. A formula is the AND of several parts called clauses, C1,…,CmC_1, \dots, C_m. So a formula ϕ\phi has the form

ϕ=C1∧⋯∧Cm.\phi = C_1 \wedge \cdots \wedge C_m.

Each clause also has a specific shape. A clause is the OR of literals, which are variables or negations of variables, enclosed in parentheses. So (x1∨¬x2)(x_1 \vee \neg x_2) is a valid clause, while (x1∧¬x2)(x_1 \wedge \neg x_2) is not, because it uses AND. A complete example formula is

ϕ=(x1∨x2∨x3)∧(¬x1)∧(x1∨¬x2∨x3)∧(x2∨¬x3).\phi = (x_1 \vee x_2 \vee x_3) \wedge (\neg x_1) \wedge (x_1 \vee \neg x_2 \vee x_3) \wedge (x_2 \vee \neg x_3).

A variation of SAT is k-SAT, where each clause has at most kk literals. The formula above is a 3-SAT instance but not a 2-SAT instance. In all these problems, for a formula to be true every clause must be true, so at least one literal (of the form xix_i or ¬xi\neg x_i) in each clause must be true.

An assignment is a way of setting the variables to true or false. This problem concerns a variation of 3-SAT in which a valid assignment must have exactly 1 or exactly 3 true literals in each clause. Given a formula, your task is to decide whether a valid assignment exists under this extra restriction. If a valid assignment exists, you must print the lexicographically largest one. Lexicographic order is defined as follows: given two different assignments, compare them at the lowest-indexed variable where they differ; the larger assignment is the one that sets that variable to true.

Input

The first line contains two integers MM and NN (1≤M,N≤20001 \le M, N \le 2000), the number of clauses and variables. Then MM lines follow, each describing one clause (see the example for the format). Consecutive clauses are separated by the string “ and”. Each clause contains at most 3 literals. Variables are written as “x” followed by a number between 1 and NN. There are no two consecutive spaces, and no space at the end of a line.

The first example describes the formula ϕ\phi above.

Output

Print one line with NN characters for the lexicographically largest valid assignment, or impossible if no valid assignment exists. The ii-th character is T if variable ii is true in the assignment and F otherwise.

Examples3

  1. Example 1

    Input
    4 3
    (x1 or x2 or x3) and
    (not x1) and
    (x1 or not x2 or x3) and
    (x2 or not x3)
    
    Expected output
    impossible
    
  2. Example 2

    Input
    5 6
    (not x1) and
    (x1 or x2 or x4) and
    (x1 or x3 or x5) and
    (not x2 or x3 or x5) and
    (x2 or x3 or not x4)
    
    Expected output
    FTTFFT
    
  3. Example 3

    Input
    1 1
    (x1 or x1 or not x1)
    
    Expected output
    F