Poj 3678 Katu puzzle (2-Sat judgment)

Source: Internet
Author: User

[Question link]

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

[Topic]

There is a directed graph G (V, E). Each side e (a, B) has a bitwise operator OP (and, or XOR) and a value C (0 or 1 ).

Can I assign a value x (0 or 1) to each vertex in this graph so that each edge e (a, B) meets XA op XB = C?

[Idea]

Each point can only take 0 or 1, apparently a 2-Sat model.

The key is how to build edges.

For two vertices A and B, A has two values a1 = 0, a2 = 1, and B also has two values b1 = 0, b2 = 1.

Then enumerate all the relationships between the two points

(A1, B1)

(A1, B2)

(A2, B1)

(A2, B2)

Then, the bitwise operators are used to check whether the matching conditions are not met. If the non-conforming conditions are met, the two edges are added to the link.

If the relationship is (A1, B1) conflict, you must add the edge A1-> B2, B1-> A2.

And so on.

[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.addEdge(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;}

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.