Preface
Recently spent a little thought study 2-sat model, read a lot of paper blog, and so on, also made a humorous on the POJ. In fact, this thing is also very fun, of course, the premise is that each problem you have serious analysis, seriously think clearly the meaning of the model, to understand why this can be done, rather than simply know how to do, on a template to do, so, is not too much to spoil this science.
About 2-sat, basically everyone will recommend two information:
Woo Jun 2-sat problem (PPT) by symmetric solution
Analysis of Zhao 2-sat solution (PDF)
I will certainly recommend, at least this is also guide me to get started things, although said at the beginning is not very understand, however, still have to worship a two Daniel. This article is mainly to summarize some of my research on 2-SAT, I do not like formulaic language, so, basically will be the vernacular statement, mainly includes the following five elements:
1, the concept of the elaboration
2, the elaboration of the algorithm
3, the interpretation of the algorithm
4, the composition of the introduction
5. Sharing of topics
Let's get down to the chase.
Concept
To know what 2-sat is, we first need to know what is the appropriate qualitative (satisfiability) problem, suitable, is suitable, fixed, is to determine, the appropriate qualitative question is to determine whether it can meet all the conditions. Or, to be sure, a solution that satisfies all conditions. Take the first three letters of English, called the SAT question. The popular SAT question statement is generally this: there are many sets, each set contains several elements, now gives some rules to take elements, to determine whether you are feasible, feasible to give a feasible solution. If there are k for the collection with the largest number of elements in all the collections, then we say this is a k-sat problem, and the 2-sat problem is the case when k=2.
Why study 2-sat? There is no doubt that it is very useful. Why not study 3-sat and even k larger case, because they have been proved to be NP complete problem, in more cases, 2-sat not only the most elements of the set contains 2 elements, but each set contains 2 elements, and these 2 elements do not allow simultaneous extraction, This article is mainly about such a special model.
algorithm
Still, my summary begins with this question. The main idea: a state-owned N-party, each party has 2 representatives in Parliament, now to form a peace commission, from each party in the Parliament to elect 1 people, a total of n people form a peace commission. It is known that there is hatred between some of the representatives, that is, they cannot be elected as members of the Peace Commission at the same time, and it is up to you to judge whether the Peace Commission satisfies the requirements. If you can, please give a solution. (POI 0106)
As you can see, the topic is very bare, in fact, 2-sat the topic is not hidden so deep, not like the network flow as difficult to find.
For this problem, of course, violence can be solved, in fact, say a little better, that is, in the figure of violence, poof, is not feeling very advanced appearance, well, this is actually a very easy to understand the algorithm.
algorithm One: PerseveranceSuppose that 1 A, 1 B are two members of party 1th, 2 A, 2B (poof, no laughter) 2nd party members of two, and there is hatred between 1A and 2A. So, what does that mean. If you choose 1A, you can only choose 2 B, if you choose 2A. That same thing can only choose 1 B, then, we can build a diagram, between 1A to 2B to catch an edge, 2A to 1B between a side, the meaning of this side is: Must.
That is, along a path, if a point is selected, then the point of this path will be selected, then, there is no feasible situation is that there is a party two members are selected, then, we only need to enumerate a bit, the data is not big, the answer can always come out.
Such a simple algorithm, put in the first said, it seems you already know that it is not the focus, but, if the topic is required to be the smallest dictionary sequence, then you can only choose this algorithm.
algorithm two: No NameThe composition is the same as the first algorithm, but in the process of the solution diagram is very beautiful optimization.
In order to communicate clearly, we first recall the concept of proposition: proposition, inverse proposition, no proposition, inverse proposition.
The concept is not said, for example is also good:
Proposition: Triangular internal angle and 180°
Inverse proposition: Triangle with internal angle and 180°
No proposition: Not the inner angle of the triangle and not 180°
Inverse No proposition: not triangular for internal angle and not 180°
One obvious conclusion is that the proposition is the same as the true or false nature of the inverse proposition (and someone has to say I'm wordy, hmm)
Maybe, you are wondering why I want to pull this thing, although I read all the information has not been introduced, and no one put these two things together, but I really think they are all the way, how to say.
Select a must choose B, then we first "reverse", from B to a, and then "No", do not choose b must not choose a, which is clearly established, that is, in a well-formed diagram, "must" is along the positive direction of the passing down, "forbidden" is to pass along the opposite direction, then each time we choose a point, It is an efficient practice to mark the opposite point (that is, another element of the same set) as not selectable, and then we pass the selection mark forward and then reverse the prohibition tag, which is a highly effective approach that conforms to test instructions, but ... Still is very troublesome, unexpectedly wants two-way transmission, moreover ... How do you know which node to choose at the beginning? 囧
Don't worry, please reach a point of consensus first:
1, if a lot of nodes are already in a strong connected component, that is, they 22 can be mutually, then it means that the selection of one, the entire strong connected components will be selected, if not select one of them, then the entire strong connected components can not be selected.
2, if a set of two elements in the same strong connected components, that is either not selected, or are selected, then the title of the selected one must not be set up.
3, no matter what two can not be selected at the same time, can not change the edge of our company is symmetrical this fact, as long as a to B has a side, then the opposite node of B to a opposite node has side.
4, the symmetry of the edge means that the symmetry of the graph, the original image symmetry means that even with strong connected components after the contraction point of the same symmetry, so the concept of the opposite node can also be applied to the strong connected components.
5, in order to describe the convenience, hereinafter referred to as strong connected components for the block, note:The real block refers to the point of the two connected components, it's just a visual representation, it's irregular.
the process of the algorithm is as follows:
composition
More specifically, I'll say it later.
Pinch point
The Tarjan algorithm shrinks the points, reversing all the edges (why. Yes, back there.
The award is feasible
Enumerates the two elements of a collection to see if they are in different blocks, and if not, to give non-line information
Record Contradictions
The contradiction here is not the question given in the two people have hatred between, so that the side is the actual existence, we say that the contradiction is that if two blocks contain two opposing nodes, that is, a set of two elements distributed in two different blocks, then the two blocks is contradictory, that is impossible to be selected at the same time, Such a contradiction does not exist in the edge, is not dependent on the input data, we want to find a block with the opposition, and save them.
Topological sorting
Sort the graph after the indented point (blocks instead of nodes)
Construction Scheme
Access all blocks in order of the topological sequence, if a block is not marked, mark it as "select", do not pass the "select" tag, mark the opposing block of the selected block as "not selected", and pass its "not selected" tag along the edge forward. (not inverse wildest.) Well, the picture has been reversed, have you ever looked seriously. 囧
It's gone. No, it's gone. I know you really want to ask why, = =,ps: Don't think I'm particularly verbose ...
Alas, I used to be so used to seeing those "not-so-wordy papers that I would like to have * *". Back to the chase, we continue.
explain
1, why to use the inverse edge to save the point after the map. --Considering that the algorithm passes the tag, the "Select" tag is not passed, that is, the forward edge is not used, the pass is "no selection" mark, that is, to go is the opposite side, but the adjacent table storage edge, how to walk the opposite edge of it. So since the positive side is useless, it is good to save the whole picture in turn, and the inverse diagram is really good. Why, look down.
2, why do you want to sort the topology? --should not come or come, this is really a disgusting problem, even if I write now, I can not have a deep recognition of it, you know, some things, even if you do not say why it is so, but you can feel, can identify, can seize its direction, convinced of its right, but, Some things, even if you have been able to say why, or can not have a deep sense of identity, always feel a kind of uncomfortable, uncomfortable feeling in, less nonsense, continue.
--Observe the image below (note: This image is the original, non-indented back image)
If we do not select the block according to the topological sequence: access to a, found that it is unmarked, so it is marked as selectable, and then the A ' is marked as not selectable, then, to mark the delivery, but the problem arises, if the "select" tag is passed, then B and B ' will be marked as a choice, if the Tags are passed, then B and B ' will be marked as not selected, and this choice is not selected, it is not contradictory. Is it because there is no solution to this figure? But according to our judgment of the standard of no solution, that is, the two opposing nodes in the same block, the figure is a solution, there is no node can be mutual, the reason is because our construction method is not correct, a in the entire edge of the upstream zone, first it is determined as the choice of the original image of the impact is too big, There is no guarantee that the right decision will be made next. Therefore, the correct method should be in accordance with the topological order, from the beginning of the impact of a small choice, after turning to the opposite side, a ' will be in the first topological sequence, the A ' is marked as a choice, the A is not selected, can not be passed, and B is marked as a choice, B ' is not selected, of course, b Mark as a choice is also possible, this completely look at the topology sorting rules, after all, we are access to an unmarked to mark it as a choice, then who first appeared who was chosen, the law of nature haha.
3. Why only the "do not select" tag is passed, and the "select" tag is not passed. --remember our selection rules. It seems to have been repeated many times ... Find an unmarked, mark it as a selection, then mark the opposing block as not selected, then pass the non-select tag, if we select B, then its descendant Node A ' (here is the descendant of the original image, as shown above) must have been selected. Why is it. If its descendant node A ' is not selected, then its descendant node of the opposite node A must be selected, according to the symmetry of the edge, a ' is a descendant node of B, then B ' is a descendant node, since a is selected, then B ' also to be selected, then according to the order, B will be marked as not selectable, Then when we access to B, it will not be identified as unmarked, it does not exist to mark it as a choice, which is contradictory to the hypothesis, it is obviously not feasible. So, since we choose a node, its descendants have been selected, then we do not need to "select" The token is passed, moreover, to mark the time we have been saved is a counter-map, even if you want to pass also no way ... 囧 In general, although we do not pass the selection tag, the nature of this transfer is satisfying.
4. When the labeling method constructs the scheme, why not select the two elements of a collection at the same time. --Well, I think this is the same as the previous question, if you still have doubts, please read it again.
5. When the labeling method constructs a scheme, why not mark the same set of two elements as not selected. --we may as well choose a block and then mark its opposing block as not selected this behavior is called the direct tag, the opposite block of the "do not select" tag is passed when the tag block is called the indirect tag, the following is easy to talk about ...
(1) If the two opposing blocks are marked as selected by a block, they are also directly marked as not selected:
So let's think about why they are labeled directly. Of course, because they have a node that is labeled as a block of choice, it is recorded in the step of record contradiction, so it will be so, but then a closer look at what is labeled as the selection of the block in which the opposing nodes of a two nodes are distributed in different blocks. Do you remember the consensus we reached? The symmetry of the edge means the symmetry of the block, then these two blocks that are directly labeled as not selected should not be separated, and this is not true. In other words, these two blocks are marked as "not selectable"not all of them .Directly labeled.
It also illustrates the important point that a block that is directly contradictory to a block exists and exists only one, so we are not using the adjacency table for the conflicting block, but just an array.
(2) if the two opposing blocks are marked as a selection, one of the blocks is directly labeled and the other is indirectly labeled:
Because one of them is directly labeled, so there is a pair of opposing nodes, and the other is directly marked with this piece for the opposing block (according to the premise), then appeared in the block directly labeled in the two nodes, their opposing nodes scattered in two blocks, which is contradictory. That is, these two blocks marked as "not selected"are notis directly marked.
(3) if the two opposing blocks are marked as selected by a block, they are also indirectly marked as not selectable:
So, the opposing block of the block that is labeled as the selection (assuming O), it has a path to the two blocks (assuming A,a '), because there is no selection tag passing (here is the inverse diagram), but according to the symmetry of the graph, since O to a has a path, then a ' goes to O, however, O to a ' also has a path, then two blocks is obviously a block, which is inconsistent with the hypothesis. In other words, these two blocks marked "not selectable" are alsoare notIndirectly tagged.
So, in general, this is not the case in direct and indirect markup after you mark a block as "select." That happens when the different blocks are labeled "select".
(4) All two opposing blocks are marked as not selected throughout the operation.
At this point, we have been able to determine that in a selection and transfer process, there is no such situation, if placed in the entire construction process, we can also be assured that any one of the two opposing blocks will not be directly marked by the other block is not optional, if a block has been marked as not selectable, Then only the opposing block of it is marked as optional, and it is marked directly, and those that may again be marked as not selectable will die under its indirect tag ... Well, that's the claim of being taken to mark others, why. Because the graph is symmetrical.
The question was settled in such a happy way.
6, all said to use symmetry to solve 2-sat problem, in the end where the use of symmetry. --Yes, this question is really stupid, do not know whether you have the same idea as me, at least I have struggled for a long time, alas, let me sigh again. Stupid question. Although the algorithm is not directly used to achieve the symmetry, but the symmetry is the cornerstone of the algorithm AH. If the original is not symmetrical. That front of the proof is not set, then the correctness of the algorithm can not be guaranteed. is really 2 of the problem, who said must be in the algorithm in the process of expression to use Ah, alas, really 2 dead.
Model
Of course, I still very much hope you see here already understand, otherwise, must be I narrate too not standard. = = If you do not understand, or to understand the better, after all, (the word is not, omitted) if you think 2-sat is only the peace commission such a topic, then you are wrong, but you think it is much more than the Peace Commission, then you are wrong, in fact, the common model is not too much.
Model One: Both (A, B) cannot be taken simultaneouslyThen select a can only choose b ', select B can only choose a '
The Edge a→b ', B→a '
Model Two: Both (A, B) cannot be taken simultaneouslyThen select a ' can only select B, select B ' can only select a
Edge A ' →b,b ' →a
model Three: Both (A, b) are either taken or not takenThen select a, you can only select B, select B can only select a, select a ' can only select B ', select B ' can only choose a '
Edge A→b,b→a,a ' →b ', B ' →a '
model four: both (A,a ') must take AWell, then, how to say it. Let's talk about even the edge.
Edge A ' →a
Did you figure out why? Maybe you're thinking, wow, this will ensure that in the inverse diagram A in the topological order of the front, and then will first select a, hehe, you are wrong.
Although I have seen this explanation in some people's blogs, I am very responsible to tell you that this is not comprehensive.
From a ' to a, we want not only the topological order, but also the feasibility or not. In the 2-sat diagram, if the graph is feasible, it means that if there is a path from a to a ' there is no path to a ' to a, because if there is a path, they become a block, will not be judged to be feasible, if a to a ' is a path, in the inverse diagram, a ' to a has a path, then the topology, A ' will be marked as ' select ' because of the front, and the condition is not met. And, we should be convinced that the multi-case of the solution depends on the topological ordering of the multi-case, not according to the topological order, the solution is wrong, or is not established, that is, we select a topological sequence first, it will lead to the other constraints are not established, then we introduced a ' to a ' this side, a and a ' In the same piece, the algorithm reports non-line information. If the original image satisfies a to a ' have path, however a ' to a without path, we add an edge, just to determine its solution, but does not affect the topological ordering, only when a to a ' between no path at all, it will affect its topological ordering, so the truth is like this.
Is not someone has begun to doubt, said so long symmetry, the entire algorithm is dependent on the symmetry to be established, then how to introduce an edge to make the graph asymmetric algorithm is also set up. The question is, in fact, very well explained, and think carefully about what symmetry guarantees. It ensures that the original has a solution, it must be constructed. Now we have introduced an edge a ' →a, if through the above judgment, let it no solution, then obviously there is no effect on the back, after all, the algorithm has not been carried out, but also what impact. If there is a solution. That shows what. This shows that a ' to a ' exists in the original path, or a ' to a ' there is no path at all. If there is a path, then I add a more of a difference. Does it affect any step of the algorithm? Obviously not, if there is no path. No path means who is possible in the topological sequence, after having the edge (the inverse is a→a '), a will be marked as "select" in the topological sequence, then we will mark a ' to a ', at this point, a ' to a is no way to go, it simply cannot access to a, so in this process of marking , this path does not affect any symmetry of the graph at all, and after the topology is sorted, you can completely do so when it does not exist.
The model is actually all the same, or look at their own to change, such as model one and model two is actually the same, just for the point of difference. Generally, if 2-sat feel is obvious, use 2-sat to do well, however, remember to consider how to reduce the complexity of composition OH. If the complexity is too high, it's better to go another way.
Topics
All originated from POJ, we can practice on their own, if you want to shoot what, there are procedures, the topics are sorted from simple to simple order (poof ...). In fact, it is recommended to do the problem sequence), we, together good refueling.
POJ 3207
POJ 3683
POJ 3678
POJ 3648
POJ 2723
POJ 2749
Finish