2-sat建边问题

In a country (my memory fails to say which), the candidates {1, 2 ..., N} are running in the parliamentary election. An opinion poll asks the question "For any two candidates of your own choice, which election result would make you happy?". The accepted answers are shown in the table below, where the candidates i and j are not necessarily different, i.e. it may happen that i=j. There are M poll answers, some of which may be similar or identical. The problem is to decide whether there can be an election outcome (It may happen that all candidates fail to be elected, or all are elected, or only a part of them are elected. All these are acceptable election outcomes.) that conforms to all M answers. We say that such an election outcome is perfect. The result of the problem is 1 if a perfect election outcome does exist and 0 otherwise.

Input

Each data set corresponds to an instance of the problem and starts with two integral numbers: 1≤N≤1000 and 1≤M≤1000000. The data set continues with M pairs ±i ±j of signed numbers, 1≤i,j≤N. Each pair encodes a poll answer as follows: 

Accepted answers to the poll questionEncoding
I would be happy if at least one from i and j is elected.+i +j
I would be happy if at least one from i and j is not elected.-i -j
I would be happy if i is elected or j is not elected or both events happen.+i -j
I would be happy if i is not elected or j is elected or both events happen.-i +j



The input data are separated by white spaces, terminate with an end of file, and are correct.

Output

For each data set the program prints the result of the encoded election problem. The result, 1 or 0, is printed on the standard output from the beginning of a line. There must be no empty lines on output.

Sample Input

3 3  +1 +2  -1 +2  -1 -3 
2 3  -1 +2  -1 -2  +1 -2 
2 4  -1 +2  -1 -2  +1 -2  +1 +2 
2 8  +1 +2  +2 +1  +1 -2  +1 -2  -2 +1  -1 +1  -2 -2  +1 -1

Sample Output

1
1
0
1

Hint

For the first data set the result of the problem is 1; there are several perfect election outcomes, e.g. 1 is not elected, 2 is elected, 3 is not elected. The result for the second data set is justified by the perfect election outcome: 1 is not elected, 2 is not elected. The result for the third data set is 0. According to the answers -1 +2 and -1 -2 the candidate 1 must not be elected, whereas the answers +1 -2 and +1 +2 say that candidate 1 must be elected. There is no perfect election outcome. For the fourth data set notice that there are similar or identical poll answers and that some answers mention a single candidate. The result is 1.

这里如果两个都是正数,那么建立两个边,a0->b1和b0->a1代表不选a0的话一定要选b1的,同理。

如果都负,至少不选一个,那么a1->b0,b1->a0带表选a1一定不选b0,同理。

如果a正b负,那么证明选a或不选b,那么a0->b0,b1->a1,同理。

...

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=1000000+10;
struct TwoSAT
{
    int n;
    vector<int> G[maxn*2];
    int S[maxn*2],c;
    bool mark[maxn*2];

    bool dfs(int x)
    {
        if(mark[x^1])
            return false;
        if(mark[x])
            return true;
        mark[x]=true;
        S[c++]=x;
        for(int i=0; i<G[x].size(); i++)
            if(!dfs(G[x][i]))
                return false;
        return true;
    }

    void init(int n)
    {
        this->n=n;
        for(int i=0; i<n*2; i++)
            G[i].clear();
        memset(mark,0,sizeof(mark));
    }

    void add_clause(int x,int xval,int y,int yval)//这里的函数做了修改,只加单向边
    {
        x=x*2+xval;
        y=y*2+yval;
        G[x].push_back(y);
    }

    bool solve()
    {
        for(int i=0; i<2*n; i+=2)
            if(!mark[i] && !mark[i+1])
            {
                c=0;
                if(!dfs(i))
                {
                    while(c>0)
                        mark[S[--c]]=false;
                    if(!dfs(i+1))
                        return false;
                }
            }
        return true;
    }
} TS;
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        TS.init(n);
        int a,b,c;
        char op[10];
        int str1;
        int str2;
        for(int i=0; i<m; i++)
        {
            scanf("%d%d",&str1,&str2);
            a=abs(str1);
            b=abs(str2);
            a--;
            b--;
            if(str1>0&&str2>0)
            {
                TS.add_clause(a,0,b,1);
                TS.add_clause(b,0,a,1);
            }
            else if(str1<0&&str2<0)
            {
                TS.add_clause(b,1,a,0);
                TS.add_clause(a,1,b,0);
            }
            else if(str1>0&&str2<0)
            {
                TS.add_clause(a,0,b,0);
                TS.add_clause(b,1,a,1);
            }
            else
            {
                TS.add_clause(b,0,a,0);
                TS.add_clause(a,1,b,1);
            }
        }
        if(TS.solve())
            printf("1\n");
        else
            printf("0\n");

    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值