ZOJ 3656 2-sat模板题

//2012长春现场赛

很裸的2-sat  因为是位运算,所以 把b[i] 的 每一位都利用 2-sat 判断一次。 

#include<stdio.h>
#include<string.h>
#include <cmath>
#include<algorithm>
#include <vector>
#include <iostream>
#include <stack>
#define fr(i,s,n) for(int i=s;i<n;i++)
#define fi freopen("in.txt","r",stdin)
#define cl(a) memset(a,0,sizeof(a))
using namespace std;
typedef long long ll;
const int N=510*4;
int b[510][510];

vector<int> g[N];
int tot,Index;
int low[N],dfn[N],instack[N];
int belong[N];

stack<int> st;
class Sol
{
public:
    int n;
    void init()
    {
        tot=Index=0;
        while(!st.empty()) st.pop();
        fr(i,0,n*2) g[i].clear();
        cl(dfn),cl(belong),cl(low),cl(instack);
    }
    bool read()
    {
        if (scanf("%d",&n)==EOF) return 0;
        fr(i,0,n)
        {
            fr(j,0,n)
            {
                scanf("%d",&b[i][j]);
            }
        }
        return 1;
    }
    bool check1()
    {
        fr(i,0,n)
        {
            if (b[i][i]!=0) return 0;
        }
        return 1;
    }
    void build()
    {
        fr(i,0,n)
        {
            fr(j,0,n)
            {
                if (i==j) continue;
                if (b[i][j]&1)
                {
                    if (i%2==1 && j%2 == 1)
                    {
                        g[ n+i ].push_back(j);
                        g[ n+j ].push_back(i);
                    }
                    else if (i%2==0 && j%2==0)
                    {
                        g[i].push_back(j);
                        g[j].push_back(i);
                       
                    }
                    else
                    {
                        g[i].push_back(j+n);
                        g[j].push_back(i+n);
                        g[j+n].push_back(i);
                        g[i+n].push_back(j);
                    }
                }
                else
                {
                    if (i%2==1 && j%2 == 1)
                    {
                        g[i+n].push_back(j+n);
                        
                        g[j+n].push_back(i+n);
                    }
                    else if (i%2==0 && j%2==0)
                    {
                        g[i].push_back(j+n);
                        g[j].push_back(i+n);
                    }
                    else
                    {
                        g[i].push_back(j);
                        g[j].push_back(i);
                        g[i+n].push_back(j+n);
                        g[j+n].push_back(i+n);
                    }
                }
            }
        }
    }
    void tarjan(int u)
    {
        dfn[u] = low[u] = ++Index;
        instack[u] = 1;
        st.push(u);
        int v,size = g[u].size();
        for(int i = 0; i < size; i++)
        {
            v = g[u][i];
            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])
        {
            tot++;
            do
            {
                v = st.top();
                st.pop();
                belong[v] = tot;  //同在一个belong集
                instack[v] = 0;
            }
            while(v != u);
        }
    }

    bool sol()
    {
        if (!check1()) return 0;
        fr(k,0,32)
        {
            init();
            build();
            fr(i,0,2*n)
            if(!dfn[i]) tarjan(i);
            fr(i,0,n)
            if(belong[i] == belong[i+n]) return 0;
            fr(i,0,n)
            {
                fr(j,0,n)
                {
                    b[i][j]>>=1;
                }
            }
        }
        return 1;
    }
};

int main()
{
    Sol s;
    while(s.read())
    {
        bool flag=s.sol();
        if (flag) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值