POJ3905 Perfect Election 2-SAT

裸的2SAT,搞之。。。。

Perfect Election
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 402 Accepted: 201

Description

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
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>

using namespace std;

#define MAXN 4400

struct node
{
	int to,next;
};

node edge[90000100];
int head[MAXN],en,n,m;

void add(int a,int b)
{
	edge[en].to=b;
	edge[en].next=head[a];
	head[a]=en++;
}

int low[MAXN],dfn[MAXN];
int stack[MAXN],top,set[MAXN],col,num;
bool vis[MAXN],instack[MAXN];

void tarjan(int u)
{
	vis[u]=1;
	dfn[u]=low[u]=++num;
	instack[u]=true;
	stack[++top]=u;
	for(int i=head[u];i!=-1;i=edge[i].next)
	{
		int v=edge[i].to;
		if(!vis[v])
		{
			tarjan(v);
			low[u]=min(low[u],low[v]);
		}
		else
			if(instack[v])
				low[u]=min(dfn[v],low[u]);
	}
	if (dfn[u]==low[u])
	{
		int j;
		col++;
		do
		{
			j=stack[top--];
			instack[j]=false;
			set[j]=col;
		}
		while (j!=u);
	}
}

void solve()
{
	int i;
	top=col=num=0;
	memset(instack,0,sizeof(instack));
	memset(vis,0,sizeof(vis));
	memset(set,-1,sizeof(set));
	for (i=1;i<=2*n;i++)
		if (!vis[i])
			tarjan(i);
}

bool twosat()
{
    solve();
    for(int i=1;i<=n;i++)
        if(set[i]==set[i+n]) return false;
    return true;
}

int main()
{
    int s1,s2;
    while(~scanf("%d%d",&n,&m))
    {
        memset(head,-1,sizeof(head));
        en=0;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&s1,&s2);
            int a=abs(s1);
            int b=abs(s2);

            if(s1>0 && s2>0)
            {
                add(a+n,b);
                add(b+n,a);
            }
            if(s1<0 && s2<0)
            {
                add(a,b+n);
                add(b,a+n);
            }
            if(s1>0 && s2<0)
            {
                add(a+n,b+n);
                add(b,a);
            }
            if(s1<0 && s2>0)
            {
                add(a,b);
                add(b+n,a+n);
            }
        }
        if(twosat())
            printf("1\n");
        else
            printf("0\n");
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值