Algorithm: POJ 3678 katu Puzzle (2-sat judgment)

Source: Internet
Author: User
Tags bitwise bool int size min strcmp

"The main effect of the topic"

There is a direction graph G (v,e), each side E (A,B) has a bitwise operator OP (and, or or XOR) and a value C (0 or 1).

Ask if you can assign a value of x (0 or 1) to each point on the graph so that each edge e (a,b) satisfies Xa op Xb = c

Ideas

Each point can take only 0 or 1, obviously the 2-SAT model.

The key is how to build the edge.

For two points A and B, a has two values a1=0,a2=1, B also has two values b1=0, B2=1.

Then enumerate all the relationships of these two points

(A1, B1)

(A1, B2)

(A2, B1)

(A2, B2)

Then according to the bitwise operator to see if each relationship is eligible or not, if not meet the relationship when the contradiction is, to add two edges

The assumption is the relationship (A1,B1) Contradiction, then we should add edge a1->b2, B1->A2 can be.

by analogy.

Code

#include <iostream> #include <cstdio> #include <cstring> using namespace std;  
    
typedef long long Int64;  
const int MAXN = 2010;  
const int VN = MAXN*2;  
    
const int EN = 4000010;  
    
int n, m;  
        Class graph{Public:void init () {size = 0;  
    Memset (Head,-1, sizeof (head));  
        } void Addedge (int u, int v) {e[size].v = v;  
        E[size].next = Head[u];  
    Head[u] = size++;  
    } public:int size;  
    int HEAD[VN];   
    struct edge{int V, next;  
}e[en];  
    
}g;   
        Class two_sat{Public:bool Check (const graph&g, const int n) {SCC (g, N);  
        for (int i=0; i<n; ++i) if (belong[i*2] = = Belong[i*2+1]) return false;  
    return true;  
        } private:void Tarjan (const graph&g, const int u) {int V;  
        Dfn[u] = low[u] = ++idx;  
        sta[top++] = u; Instack[u] = true;  
            for (int e=g.head[u]; E!=-1 e=g.e[e].next) {v = g.e[e].v;  
                if (dfn[v] = =-1) {Tarjan (g, v);  
            Low[u] = min (Low[u], low[v]);  
            }else if (Instack[v]) {Low[u] = min (Low[u], dfn[v]);  
            } if (low[u] = = Dfn[u]) {++bcnt;  
                do{v = sta[--top];  
                INSTACK[V] = false;  
            BELONG[V] = bcnt;  
        }while (U!= v);    
        } void SCC (const GRAPH&G, const int n) {top = idx = bcnt = 0;  
        memset (DFN,-1, sizeof (DFN));  
        memset (instack, 0, sizeof (instack));  
        for (int i=0; i<2*n; ++i) {if (dfn[i] = = 1) Tarjan (g, I);  
    } private:int top, idx, bcnt;  
    int STA[VN];  
    int DFN[VN];  
    int LOW[VN];  
    int BELONG[VN];  
BOOL INSTACK[VN];}sat;  
    int main () {int u, V, W;  
    Char op[5];  
        while (~SCANF ("%d%d", &n, &m)) {g.init ();       
            for (int i=0; i<m; ++i) {scanf ("%d%d%d%s", &u, &v, &w, op);     if (!strcmp (OP, "and")) {if (W) {G.addedge (u*2, v*2+1), G.addedge (v*2, u*2+1);   0, 0 G.addedge (u*2, v*2), G.addedge (v*2+1, u*2+1);       0, 1 G.addedge (u*2+1, v*2+1), G.addedge (v*2, u*2);     1, 0}else{G.addedge (u*2+1, v*2), G.addedge (v*2+1, u*2); 1, 1}}else if (!strcmp (OP, "OR") {if (W) {G.A     Ddedge (u*2, v*2+1), G.addedge (v*2, u*2+1);   0, 0}else{g.addedge (u*2, v*2), G.addedge (v*2+1, u*2+1); 0, 1 G.addedge (u*2+1, v*2+1), G.addedge (v*2, u*2);     1, 0 G.addedge (u*2+1, v*2), G.addedge (v*2+1, u*2); 1, 1}}else{//XOR if (w) {G.addedge (u*2, V     *2+1), G.addedge (v*2, u*2+1);     0, 0 G.addedge (u*2+1, v*2), G.addedge (v*2+1, u*2);   1, 1}else{g.addedge (u*2, v*2), G.addedge (v*2+1, u*2+1);       0, 1 G.addedge (u*2+1, v*2+1), G.addedge (v*2, u*2);  
        1, 0}}} if (Sat.check (g, N)) puts ("YES");  
    Else puts ("NO");  
return 0; }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.