Algorithm: POJ 3905 Perfect election (2-sat simple to judge)

Source: Internet
Author: User
Tags int size min

Ideas

2-sat a simple judgment question

Code

#include <iostream> #include <queue> #include <cstdio> #include <cstring> using namespace std  
    
;  
const int MAXN = 1010;  
const int VN = MAXN*2;  
    
const int EN = VN*VN*2;  
    
     
int n, m;  
struct edge{int V, next;  
    
};  
    struct graph{int size, HEAD[VN];  
    Edge E[en];  
    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++;  
    
}}g;  
        Class two_sat{Public:bool Check (const graph& g, const int n) {SCC (g, 2*n);  
        for (int i=0; i<n; ++i) if (belong[i] = = Belong[i+n]) return false;  
    return true;   
        } private:void Tarjan (const graph& g, const int u) {int V;  
        Low[u] = dfn[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 (dfn[u] = = Low[u]) {++bcnt;  
                do{v = sta[--top];  
                INSTACK[V] = false;  
            BELONG[V] = bcnt;  
        }while (U!= v);  
        } void SCC (const graph& g, const int n) {bcnt = idx = top = 0;  
        memset (DFN,-1, sizeof (DFN));  
        memset (instack, 0, sizeof (instack));  
    for (int i=0; i<n; ++i) if (dfn[i] = = 1) Tarjan (g, I);  
    } private:int idx, top, bcnt;  
    int DFN[VN], low[vn], BELONG[VN], STA[VN];  
BOOL INSTACK[VN];  
    
}sat;  
    int main () {int A, B;  
    
       while (~SCANF ("%d%d", &n, &m)) { G.init ();  
    
            for (int i=0; i<m; ++i) {scanf ("%d%d", &a, &b);  
                if (a>0 && b>0) {//+ +--a;--b;  
                G.addedge (a+n, b);  
    
            G.addedge (B+n, a);    
                }else if (a>0 && b<0) {//+-B =-C; --a;  
                --b;  
                G.addedge (A+n, b+n);  
    
            G.addedge (b, a);  
                }else if (a<0 && b>0) {//-+ a =-A; --a;  
                --b;   
                G.addedge (A, b);  
    
            G.addedge (B+n, a+n);  
                }else if (a<0 && b<0) {//--A =-A; b =-A; --a;  
                --b;  
                G.addedge (A, b+n);  
            G.addedge (b, a+n);  
        } if (Sat.check (g, N)) puts ("1");  
        
    Else puts ("0");  
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.