2-sat Getting Started

Source: Internet
Author: User

First, the pre-position skills

> Tarjan Cutting Point

> High School Mathematics elective 2-1 proposition and logical relationship (like this name)

> Raspberry.

(This article uses && Representation and | | Indicates or!x that the x is different from the normal mathematical symbol. Please note)

Second, K-sat

> Q: Why do you talk about K-sat before 2-sat?

Because K-sat was proven to be np-complete problem

So the mob will not take care of it ...

> Q: So what is the "SAT"?

an examination sponsored by the American University Council

The SAT is called the problem of the satisfaction of the Boolean equation

is to judge the correctness of a set of logical relationships that are satisfied at the same time ...

such as a&& (!a) is not satisfied

Another example a&&b| | c&&!d&& (...)

We give a special Boolean equation a name called the fit Paradigm .

is the shape of (a | | !b | | c) && (d | | e) && (...)

where each bool variable is called the bracketed part of the text is called a clause

If the size of each clause does not exceed K, then it is called K-sat.

Similarly, if the number of words in each clause of the combined paradigm is no more than two, then the corresponding SAT problem is also called the 2-sat problem.

Third, the solution

Consider the left side of a binary graph to indicate that the bool variable value is 0 (pseudo) to the right to represent a value of 1 (true)

If satisfying a proposition must be able to introduce another proposition then the two propositions are connected to one side

Considering the relationship between the two bool variables is nothing more than a && | |

Since we are 2-sat, we are most likely to have a | | B Two Variables

Then into the implication (launch) relationship

Definition of operation by | |

!a = B and!b = a

Then the &&

!a = A and!b = b

> Q: This ... bool variable can also satisfy both true and false?

Of course not here the introduction can be understood that if a is selected pseudo, then the end will be introduced A is true

So when we finally choose the time not to choose a for pseudo can only select a for the true to achieve our purpose

> Q: Well ... But why is it so built?

We consider the nature of the bool operation

When the proposition is set up, its inverse proposition is established and called symmetry .

Also consider the introduction of transitivity , if B is inferred from a certain variable to be true and it is true the effect is the same can infer the next variable

So the value of the bool of the point in a strongly connected component is the same (note that the bool value of the variable is not the same)

At the same time the previous q indicates that the map can handle the problem of single variable fixed value

> Q: Well ... How to find the final answer?

We run a Tarjan for the last built figure if the value of the variable is in the same strong-connected component (i.e. neither true nor pseudo) then there is no solution.

Otherwise, the answer to each point should be the one after the topological order in the truth and pseudo-values (the same as the first Q)

(So to shrink the point)

And since we're running Tarjan, it's a small number of strongly connected components behind the topological sequence.

So you don't have to build a new picture.

Four, the code

Code: (Luogu P4782)

1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <cmath>5 #defineITN int6 #defineMS (A, b) memset (a,b,sizeof a)7 #defineRep (i,a,n) for (int i = a;i <= n;i++)8 #definePer (i,n,a) for (int i = n;i >= a;i--)9 using namespacestd;TentypedefLong Longll; One ll Read () { All as=0, Fu =1; -     Charc =GetChar (); -      while(C <'0'|| C >'9') { the         if(c = ='-') Fu =-1; -c =GetChar (); -     } -      while(c >='0'&& C <='9') { +          as= as*Ten+ C-'0'; -c =GetChar (); +     } A     return  as*Fu; at } - Const intN =2000005; - //Head - intn,m; - inthead[n],nxt[n<<1],mo[n<<1],cnt =-1; - BOOLFlag[n]; in voidAdd (ITN x,inty) { -MO[++CNT] =y; toNXT[CNT] =Head[x]; +HEAD[X] =CNT; - } the intRev (intx) {returnx>n?x-n:x+N;} *  $ intDfn[n],low[n],col[n],stk[n];Panax Notoginseng intScc,idx,top; - BOOLIns[n]; the voidTarjan (intx) { +DFN[X] = low[x] = + +idx; AStk[++top] = x,ins[x] =1; the      for(inti = Head[x];i;i =Nxt[i]) { +     intsn =Mo[i]; -     if(!DFN[SN]) { $ Tarjan (SN); $LOW[X] =min (low[x],low[sn]); -}Else if(INS[SN]) low[x] =min (low[x],dfn[sn]); -     } the     if(Dfn[x] = =Low[x]) { -     intt =-1;WuyiScc++; the      while(t!=x) { -t = stk[top--]; WuCOL[T] =SCC; -INS[T] =0; About     } $     } - } -  - intMain () { An =read (); +m =read (); theRep (I,1, M) { -     intx = read (); x + = Read () *N; $     inty = read (); y + = read () *N; the Add (rev (x), y), add (rev (y), x); the     } theRep (I,1,n<<1)if(!Dfn[i]) Tarjan (i); the     BOOLFlag =1; -Rep (I,1, N)if(Col[i] = = Col[n+i]) flag =0; in     if(!flag) puts ("Impossible"); the     Else { thePuts"POSSIBLE"); AboutRep (I,1, N) printf ("%d", Col[i] > col[i+n]); thePuts""); the     } the     return 0; +}
View Code

Attention:

1. When running, consider multiple unicom blocks like Tarjan.

2. The relationship of the whole or the subject note reverse build

3. At the end of the comparison, each person's wording is different to consider greater than or less than

For examples, see Practice 1

by Prophetb

2-sat Getting Started

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.