floor Puzzle

POJ

The meaning of problems: there are \ (N \) variables \ (x_1-x_n \) , each of the possible values 0 or 1. The variable is given \ (M \) th equations, each equation of the form \ (X_a \ ) \ (OP \) \ (X_b = C \) , where \ (a, b \) is the number of two variables, \ (C \) is the number 0 or. 1, \ (OP \) is \ (and \ ) , \ (or \) , \ (XOR \) one of the three bit operation. ask whether there is a valid assignment for each variable such that all equations are true. \ (n-<= 1000, m <= 10. 6 ^ . \)

Analysis: \ (the SAT-2 \) problem, first converted into \ (the SAT-2 \) . Provided in the form of nodes \ (A \) represents \ (X_a \) values 0, node \ (a + n \) representation \ (x_a \) value of 1:

\ (A \) \ (and \) \ (B = 0 \) , then \ (x_a = 1 \) when, \ (X_b \) must be \ (= 0 \) , even directed edge \ ((a + n-, B) \) .. other cases like this think Note: be able to satisfy even the edge condition is: "If p, must q".

FIG built after ran digraph \ (Tarjan \) , if there is a node \ (I \) , \ (I \) and \ (i + n \) belong to the same strongly connected component, then there is no valid assignment a.

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define ll long long
using namespace std;
inline int read(){
    int x=0,o=1;char ch=getchar();
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')o=-1,ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*o;
}
const int N=1005;
const int M=4e6+5;
int tot,head[N],nxt[M],to[M];
int tim,top,num,dfn[N],low[N],st[M],color[N];
inline void add(int a,int b){nxt[++tot]=head[a];head[a]=tot;to[tot]=b;}
inline void tarjan(int u){
    dfn[u]=low[u]=++tim;st[++top]=u;
    for(int i=head[u];i;i=nxt[i]){
        int v=to[i];
        if(!dfn[v]){
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(!color[v])low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u]){
        color[u]=++num;
        while(st[top]!=u){
            color[st[top]]=num;
            --top;
        }
        --top;
    }
}
int main(){
    int n=read(),m=read();
    for(int i=1;i<=m;++i){
        int a=read(),b=read(),c=read();
        ++a;++b;string s;cin>>s;
        if(s[0]=='A'){
            if(c==0)add(a+n,b),add(b+n,a);
            else add(a,a+n),add(b,b+n);
        }
        else if(s[0]=='O'){
            if(c==0)add(a+n,a),add(b+n,b);
            else add(a,b+n),add(b,a+n);
        }
        else if(s[0]=='X'){
            if(c==0){
                add(a,b);add(b,a);
                add(a+n,b+n);add(b+n,a+n);
            }
            else{
                add(a,b+n);add(b,a+n);
                add(a+n,b);add(b+n,a);
            }
        }
    }
    for(int i=1;i<=n*2;++i)if(!dfn[i])tarjan(i);
    for(int i=1;i<=n;++i)
        if(color[i]==color[i+n]){
            puts("NO");return 0;
        }
    puts("YES");
    return 0;
}

Guess you like

Origin www.cnblogs.com/PPXppx/p/11592627.html