POJ 3905 Perfect Election(2-sat 模板题,3级)

M - Perfect Election
Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status
Appoint description:  System Crawler  (2012-06-23)System Crawler  (2013-05-30)

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.

哎,模板题,

很坑,数据范围尽量开大了。


#include<cstdio>
#include<cstring>
#include<iostream>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
const int mm=222110;
const int me=4e6+9;
class Edge
{
  public:int v,next;
};
class candidate
{
  public:char x[10],y[10];
}f[me];
class TWO_SAT
{
public:
  int dfn[mm],e_to[mm],stack[mm];
  Edge e[me];
  int edge,head[mm],top,dfs_clock,bcc;
  void clear()
  {
    edge=0;clr(head,-1);
  }
  void add(int u,int v)
  {
    e[edge].v=v;e[edge].next=head[u];head[u]=edge++;
  }
  void add_my(int x,int xval,int y,int yval)
  {
    x=x+x+xval;y=y+y+yval;
    add(x,y);
  }
  void add_clause(int x,int xval,int y,int yval)
  {///x or y
    x=x+x+xval;
    y=y+y+yval;
    add(x^1,y);add(y^1,x);
  }
  int tarjan(int u)
  {
    int lowu,lowv;
    lowu=dfn[u]=++dfs_clock;
    int v; stack[top++]=u;
    for(int i=head[u];~i;i=e[i].next)
    {
      v=e[i].v;
      if(!dfn[v])
      {
        lowv=tarjan(v);
        lowu=min(lowv,lowu);
      }
      else if(e_to[v]==-1)//in stack
        lowu=min(lowu,dfn[v]);
    }
    if(dfn[u]==lowu)
    {
      ++bcc;
      do{
        v=stack[--top];
        e_to[v]=bcc;
      }while(v!=u);
    }
    return lowu;
  }
  bool find_bcc(int n)
  { clr(e_to,-1);
    clr(dfn,0);
    bcc=dfs_clock=top=0;
    FOR(i,0,2*n-1)
    if(!dfn[i])
      tarjan(i);
    for(int i=0;i<2*n;i+=2)
      if(e_to[i]==e_to[i^1])return 0;
    return 1;
  }
  void getID(char*s,int&id,int&yes)
  { id=0;
    for(int i=0;s[i];++i)
    if(s[i]=='+')yes=1;
    else if(s[i]=='-')yes=0;
    else if(s[i]>='0'&&s[i]<='9')id=id*10+s[i]-'0';
  }
  void build(int n,int m)
  {
    clear();
    int a,b,c,d;
    FOR(i,1,m)
    {
     getID(f[i].x,a,b);
     getID(f[i].y,c,d);
     add_clause(a-1,b,c-1,d);
    }
    if(find_bcc(n+n))printf("1\n");
    else printf("0\n");
  }
}two;
int n,m;
int main()
{ int a,b,c,d;
 //freopen("data.in","r",stdin);
  while(~scanf("%d%d",&n,&m))
  { two.clear();
    FOR(i,1,m)
    {
      scanf("%s%s",f[i].x,f[i].y);
//        scanf("%d%d",&a,&b);
//        c=a>0?a:-a;
//        d=b>0?b:-b;
//        --c;--d;
//        if(a>0&&b>0)two.add_clause(c,1,d,1);
//        if(a>0&&b<0)two.add_clause(c,1,d,0);
//        if(a<0&&b>0)two.add_clause(c,0,d,1);
//        if(a<0&&b<0)two.add_clause(c,0,d,0);
    }
//    if(two.find_bcc(n*2))printf("1\n");
//    else printf("0\n");
    two.build(n,m);
//    if(two.find_bcc(n))printf("YES\n");
//    else printf("NO\n");
  }
  return 0;
}



你可能感兴趣的:(POJ 3905 Perfect Election(2-sat 模板题,3级))