Algorithm: HDU 4115 Eliminate the Conflict (2-sat judgment)

Source: Internet
Author: User
Tags bool cas int size min

"The main effect of the topic"

Bob and Alice play with scissors and rock cloth, one plays n round, Alice already knows what Bob wants each time, 1 for scissors, 2 for stone, 3 for cloth, then Bob makes some restrictions on Alice:

To M line, each row is a B k, if K is 0, it means that Alice A and b out of the fist must be the same, if K is 1, it means that Alice A and B punches must be different.

Once Alice broke the rules, or lost a game, Alice lost completely.

Isn't it possible to ask Alice to win?

Analysis

Because Alice could not lose once, so according to Bob's Fist, Alice can only win or draw, that is, each time there are two choices, is the 2-SAT model

Then there will be some contradictions on the assumption that the first a time can be out of A1,A2, B can be out of B1 and B2

If the first and B requirements are the same, but A1 and B1 are different, explain this contradiction, establish the connection a1->b2, B1->A2

Similarly, the first and B requirements are not the same, but A1 and B2 the same, explain this contradiction, establish link a1->b1, B2->A2

... ...

Then you can use 2-sat to judge.

Code

#include <iostream> #include <cstdio> #include <cstring> using namespace std;  
    
typedef long long Int64;  
const int MAXN = 10010;  
const int VN = MAXN*2;  
const int EN = VN*3;  
int n, m;  
int PAT[MAXN];  
    
int alice[maxn][2];  
struct edge{int V, next;  
    
};  
        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 HEAD[VN];  
Edge E[en];  
Private:int size;  
    
}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[2*i] = = Belong[2*i+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 (dfn[u] = = Low[u]) {++bcnt;  
                do{v = sta[--top];  
                INSTACK[V] = false;  
            BELONG[V] = bcnt;  
        }while (U!= v);  
        } void SCC (const graph& g, 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 ncase;  
    int Cas=1;  
    int A, b, C;  
    
    scanf ("%d", &ncase);  
        while (ncase--) {g.init ();  
        scanf ("%d%d", &n,&m);  
            for (int i=0; i<n; ++i) {scanf ("%d", &pat[i]);  
            pat[i]--;  
            Alice[i][0] = Pat[i];  
        ALICE[I][1] = (pat[i]+1)%3;  
            for (int i=0; i<m; ++i) {scanf ("%d%d%d", &a,&b,&c);  
            --a,--b;  if (c) {//A,b different if (alice[a][0]==alice[b][0]) G.addedge (a*2, b*2+1), G.addedge (B*2,  
                A*2+1);  
                if (alice[a][0]==alice[b][1]) G.addedge (a*2, b*2), G.addedge (b*2+1, a*2+1);  
                if (Alice[a][1]==alice[b][0]) G.addedge (a*2+1, b*2+1), G.addedge (b*2, a*2); if (alice[a][1]==alice[b][1]) G.addedge (a*2+1, b*2), G.addedge (b*2+1, a*2); }else{//A, B same if (alice[a][0]!=alice[b][0]) G.addedge (a*2, b*2+1), G.addedge (b*  
                2, a*2+1);  
                if (alice[a][0]!=alice[b][1]) G.addedge (a*2, b*2), G.addedge (b*2+1, a*2+1);  
                if (Alice[a][1]!=alice[b][0]) G.addedge (a*2+1, b*2+1), G.addedge (b*2, a*2);  
    
            if (alice[a][1]!=alice[b][1]) G.addedge (a*2+1, b*2), G.addedge (b*2+1, a*2);  
        } printf ("Case #%d:", cas++);  
        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.