Graph Theory: 2-Sat Template

Source: Internet
Author: User

 

1 # include <cstdio> 2 # include <vector> 3 # include <cstring> 4 using namespace STD; 5 6 const int maxn = _____; 7 8 struct twosat 9 {10 int N; 11 vector <int> G [maxn * 2]; 12 bool mark [maxn * 2]; 13 int s [maxn * 2], c; 14 15 bool DFS (int x) 16 {17 if (MARK [x ^ 1]) return false; 18 if (MARK [x]) return true; 19 mark [x] = true; 20 s [C ++] = x; 21 for (INT I = 0; I <G [X]. size (); I ++) 22 if (! DFS (G [x] [I]) return false; 23 return true; 24} 25 26 void Init (INT N) // pay attention to the number of initialization points, 27 {28 This-> N = N; 29 for (INT I = 0; I <n * 2; I ++) g [I]. clear (); 30 memset (mark, 0, sizeof (Mark); 31} 32 33 // X = xval or Y = yval34 void add_clause (int x, int xval, int y, int yval) // The number ranges from 0 ~ The n-135 {36 x = x * 2 + xval; 37 y = y * 2 + yval; 38g [x ^ 1]. push_back (y); 39G [y ^ 1]. push_back (x); 40} 41 42 bool solve () 43 {44 for (INT I = 0; I <n * 2; I ++ = 2) 45 if (! Mark [I] &! Mark [I + 1]) 46 {47 c = 0; 48 if (! DFS (I) 49 {50 while (C> 0) MARK [s [-- C] = false; 51 if (! DFS (I + 1) return false; 52} 53} 54 return true; 55} 56}; 57 58 twosat solver; 59 /////////////////////////////////////// /////////// 60 // binary search, 2-Sat usually uses 61 int L = ____, r = ____; 62 while (L <r) 63 {64 int M = L + (R-L + 1)/2; 65 if (test (M) L = m; // If M meets the condition, test returns true 66 else r = M-1; 67} 68 printf ("% d \ n ", l );

 

Graph Theory: 2-Sat Template

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.