Poj 3905 Perfect Election【2-SAT-------Tarjan强连通】

来源:互联网 发布:unity3d 镜面反射 编辑:程序博客网 时间:2024/05/17 23:02

Perfect Election

Time Limit: 5000MS

 

Memory Limit: 65536K

Total Submissions: 1382

 

Accepted: 536

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 question

Encoding

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.

Source

Southeastern European Regional Programming Contest 2008

 

这是一个神秘的国度,一些候选人正在举行选举,一项民意调查问:对于ij两个候选人,怎样的结果才是您想要的结果呢?

+i +j :i,j两人中至少有一个选上了

-i -j :i,j两人中,至少一个人没选上

+i -j :i选上了或者j没选上

-i +j :i没选上或者j选上了

对于民意调查的结果,问有没有一种选举方法使得所有民众都满意。


思路:


1、拆点,对于一个候选人来说,无非两种结果,一种是选上了,一种是没选上,那么我们将一个点拆成两个点,一个点表示选上了a,一个点表示没选上!a。


2、对于建边,对于我们要考虑的是找矛盾边。

①+i,+j 两人中至少有一个选上了,那矛盾边就是:两人都没有选上,那么建成的边就是:(!i,j)和(!j,i)

②-i,-j 两人中至少一个没选上,那矛盾边就是:两人都选上了,那么建成的边就是:(i,!j)和(j,!i)

③+i,-j i选上了或者j没选上,那矛盾边就是:i没选上,j选上了,那么建成的边就是:(j,i)和(!i,j)

④-i,+j i没选上或者j选上了,那矛盾边就是:i选上了,j没选上,那么建成的边就是:(i,j)和(!j,i)


3、求强连通,然后判断a和!a是否在一个强连通分量中,如果在,说明输出0,否则输出1


AC代码:


#include<stdio.h>#include<string.h>#include<iostream>using namespace std;int head[100000];struct EdgeNode{    int to;    int w;    int next;}e[5000000];int vis[100000];int dfn[100000];int low[100000];int color[100000];int stack[1000000];int n,m,cont,sig,cnt,tt;void init(){    cont=0;    tt=-1;    cnt=1;    sig=0;    memset(vis,0,sizeof(vis));    memset(head,-1,sizeof(head));    memset(dfn,0,sizeof(dfn));    memset(low,0,sizeof(low));    memset(color,0,sizeof(color));    memset(stack,0,sizeof(stack));}void add(int from,int to){    e[cont].to=to;    e[cont].next=head[from];    head[from]=cont++;}void Tarjan(int u){    vis[u]=1;    low[u]=dfn[u]=cnt++;    stack[++tt]=u;    for(int i=head[u];i!=-1;i=e[i].next)    {        int v=e[i].to;        if(vis[v]==0)Tarjan(v);        if(vis[v]==1)low[u]=min(low[u],low[v]);    }    if(dfn[u]==low[u])    {        sig++;        do        {            color[stack[tt]]=sig;            vis[stack[tt]]=-1;        }        while(stack[tt--]!=u);    }}void Slove(){    for(int i=1;i<=2*n;i++)    {        if(vis[i]==0)        {            Tarjan(i);        }    }    int flag=0;    for(int i=1;i<=n;i++)    {        if(color[i]==color[i+n])flag=1;    }    if(flag==1)    printf("0\n");    else printf("1\n");}int main(){    while(~scanf("%d%d",&n,&m))    {        init();        for(int i=0;i<m;i++)        {            char a,b;            int x,y;            scanf(" %c%d %c%d",&a,&x,&b,&y);            if(a=='+'&&b=='+')            {                add(x+n,y);                add(y+n,x);            }            if(a=='-'&&b=='-')            {                add(x,y+n);                add(y,x+n);            }            if(a=='+'&&b=='-')            {                add(y,x);                add(x+n,y+n);            }            if(a=='-'&&b=='+')            {                add(x,y);                add(y+n,x+n);            }        }        Slove();    }}





0 0
原创粉丝点击