zoukankan      html  css  js  c++  java
  • poj3678(two-sat)

    传送门:Katu Puzzl

    题意:n个点,点的取值可以是0或者1。m条边,有权值,有运算方式(并,或,异或),要求和这条边相连的两个点经过边上的运算后的结果是边的权值。问你有没有可能把每个点赋值满足所有边的要求。

    分析:每个点必须取一个值满足所有限制条件,明显的two-sat模型。

    AND 结果为1:建边 ~a->a,~b->b (两个数必须全为1)

    AND 结果为0:建边 b->~a,a->~b (两个数至少有一个为0)

    OR 结果为1:建边 ~a->b,~b->a (两个数至少有一个为1)

    OR 结果为0:建边 a->~a,b->~b (两个数必须全为0)

    XOR 结果为1:建边 a->~b,b->~a,~b->a,~a->b (两个数必须不同)

    XOR 结果为0:建边 a->b,b->a,~a->~b,~b->~a (两个数必须相同)

    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <cmath>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <cstdlib>
    #include <stack>
    #include <vector>
    #include <set>
    #include <map>
    #define LL long long
    #define mod 100000000
    #define inf 0x3f3f3f3f
    #define eps 1e-6
    #define N 2010
    #define FILL(a,b) (memset(a,b,sizeof(a)))
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define PII pair<int,int>
    using namespace std;
    struct edge
    {
        int v,next;
        edge() {}
        edge(int v,int next):v(v),next(next) {}
    } e[N*N/2];
    int n,m,scc,step,top,tot;
    int head[N],dfn[N],low[N],belong[N],Stack[N];
    bool instack[N];
    void init()
    {
        tot=0;step=0;
        scc=0;top=0;
        FILL(head,-1);
        FILL(dfn,0);
        FILL(low,0);
        FILL(instack,false);
    }
    void addedge(int u,int v)
    {
        e[tot]=edge(v,head[u]);
        head[u]=tot++;
    }
    void tarjan(int u)
    {
        int v;
        dfn[u]=low[u]=++step;
        Stack[top++]=u;
        instack[u]=true;
        for(int i=head[u]; ~i; i=e[i].next)
        {
            v=e[i].v;
            if(!dfn[v])
            {
                tarjan(v);
                low[u]=min(low[u],low[v]);
            }
            else if(instack[v])
            {
                low[u]=min(low[u],dfn[v]);
            }
        }
        if(dfn[u]==low[u])
        {
            scc++;
            do
            {
                v=Stack[--top];
                instack[v]=false;
                belong[v]=scc;
            }
            while(v!=u);
        }
    }
    
    void solve()
    {
        for(int i=0; i<2*n; i++)
            if(!dfn[i])tarjan(i);
        bool flag=true;
        for(int i=0; i<n; i++)
        {
            if(belong[i<<1]==belong[i<<1^1])
            {
                flag=false;
                break;
            }
        }
        if(flag)puts("YES");
        else puts("NO");
    }
    int main()
    {
        int a,b,c,u,v;
        char op[10];
        while(scanf("%d%d",&n,&m)>0)
        {
            init();
            for(int i=0;i<m;i++)
            {
                scanf("%d%d%d%s",&a,&b,&c,op);
                if(op[0]=='A')
                {
                    if(c)
                    {
                        addedge(a<<1^1,a<<1);
                        addedge(b<<1^1,b<<1);
                    }
                    else
                    {
                        addedge(a<<1,b<<1^1);
                        addedge(b<<1,a<<1^1);
                    }
                }
                else if(op[0]=='O')
                {
                    if(c)
                    {
                        addedge(a<<1^1,b<<1);
                        addedge(b<<1^1,a<<1);
                    }
                    else
                    {
    
                       addedge(a<<1,a<<1^1);
                       addedge(b<<1,b<<1^1);
                    }
                }
                else
                {
                    if(c)
                    {
                        addedge(a<<1,b<<1^1);
                        addedge(b<<1,a<<1^1);
                        addedge(a<<1^1,b<<1);
                        addedge(b<<1^1,a<<1);
                    }
                    else
                    {
                        addedge(a<<1,b<<1);
                        addedge(b<<1,a<<1);
                        addedge(a<<1^1,b<<1^1);
                        addedge(b<<1^1,a<<1^1);
                    }
                }
            }
            solve();
        }
    }
    View Code
  • 相关阅读:
    没有spring如何使用注解下篇
    在没有spring框架如何使用注解上篇
    oracle11g里sqldeveloper不能打开的问题
    java代码换行
    枚举接口Enumeration
    java开发环境的搭建(上班笔记01)
    2013.12.12-2013.12.22面试
    2013.12.12-2013.12.20面试
    supervisor superlance
    Laravel 返回日期问题2021-07-23T05:56:03.000000Z
  • 原文地址:https://www.cnblogs.com/lienus/p/4284299.html
Copyright © 2011-2022 走看看