POJ2723GetLuffyOut (2

Source: Internet
Author: User
POJ2723GetLuffyOut (2-SAT) poj. orgproblem? Id2723 question: you have 2n keys in your hand, which are divided into n pairs, each of which consists of two different keys. now there are M doors in order, and each door has two locks. You only need to open one of them to open the door. now you need to press the key in your hand

POJ 2723 Get Luffy Out (2-SAT) http://poj.org/problem? Id = 2723 question: you have 2n keys in your hand, which are divided into n pairs, each of which consists of two different keys. now there are M doors in order, and each door has two locks. You only need to open one of them to open the door. now you need to press the key in your hand

POJ 2723 Get Luffy Out (2-SAT)

Http://poj.org/problem? Id = 2723

Question:

You have 2n keys in your hand. These 2n keys are divided into n pairs, each of which consists of two different keys. now there are M doors in order, and each door has two locks. You only need to open one of them to open the door. now you need to use the key in your hand to open the door in sequence, but for the two keys that belong to the same group, if you use key A, you will never use key B again. how many doors can you open in order?

Analysis:

There are 2n keys first, so each key corresponds to two nodes: Use or.

For two keys a and B in the same group: (a = 0 indicates that key a is selected, and a = 1 indicates that key a is not selected)

A uses-> B does not use a = 0-> B = 1

Use B-> a, that is, B = 0-> a = 1

For a door with a lock (key) a and a lock (key) B, we must select at least one of the two locks, so there is an edge: a = 1-> B = 0 and B = 1-> a = 0.

In this way, we can enumerate the number of doors that can be opened sequentially. When num is set to 5, we can add the edges generated by the first five locks to see if there is any solution to the 2-SAT problem. if num = 5 has a solution, try num = 6. if num = 5, then the larger num will certainly have no solution. (Note: In this process, initialize the mark array)

AC code: (the answer is not binary. If binary is used, it should be faster)

# Include
 
  
# Include
  
   
# Include
   
    
# Define deusing namespace std; const int maxn = 2000 + 100; struct TwoSAT {int n; vector
    
     
G [maxn * 2]; int S [maxn * 2], c; bool mark [maxn * 2]; bool dfs (int x) {if (mark [x ^ 1]) return false; if (mark [x]) return true; mark [x] = true; S [c ++] = x; for (int I = 0; I
     
      
N = n; for (int I = 0; I <2 * n; I ++) G [I]. clear (); memset (mark, 0, sizeof (mark);} void add_clause (int x, int xval, int y, int yval) {x = x * 2 + xval; y = y * 2 + yval; G [x]. push_back (y);} bool solve () {for (int I = 0; I <2 * n; I + = 2) if (! Mark [I] &! Mark [I + 1]) {c = 0; if (! Dfs (I) {while (c> 0) mark [S [-- c] = false; if (! Dfs (I + 1) return false ;}} return true ;}} TS; int main () {int n, m; while (scanf ("% d ", & n, & m) = 2 & n) {TS. init (n * 2); for (int I = 0; I
      
     
    
   
  
 

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.