POJ 3905 - Perfect Election 简单2-sat

来源:互联网 发布:linux 内核调试kgd 编辑:程序博客网 时间:2024/05/18 02:48

          题意:

                    对N个人问2个问题. 每个人选择的问题与其快乐否有如下的关系                

Accepted answers to the poll questionEncodingI would be happy if at least one from i and j is elected.+i +jI would be happy if at least one from i and j is not elected.-i -jI would be happy if i is elected or j is not elected or both events happen.+i -jI would be happy if i is not elected or j is elected or both events happen.-i +j
                     问有没有一种安排问题的方法让每个人都快乐...

          题解:

                     很基础的2-sat构图..


Progarm:

#include<iostream>#include<stdio.h>#include<cmath>#include<queue>#include<stack>#include<string.h>#include<map>#include<set>#include<algorithm>#define oo 1000000007#define MAXN 1005<<1#define MAXM 1000005<<1#define ll long longusing namespace std;   struct node{        int y,next;}line[MAXM];int Lnum,_next[MAXN],tp[MAXN],dfn[MAXN],low[MAXN],tpnum,DfsIndex;bool instack[MAXN];stack<int> mystack;void addline(int x,int y){       line[++Lnum].next=_next[x],_next[x]=Lnum,line[Lnum].y=y;}void tarjan(int x){       dfn[x]=low[x]=++DfsIndex;       mystack.push(x),instack[x]=true;       for (int k=_next[x];k;k=line[k].next)       {               int y=line[k].y;               if (!dfn[y])               {                    tarjan(y);                    low[x]=min(low[x],low[y]);               }if (instack[y])                     low[x]=min(low[x],dfn[y]);       }       if (dfn[x]==low[x])       {               tpnum++;               do               {                       x=mystack.top();                       mystack.pop();                       tp[x]=tpnum;                       instack[x]=false;               }while (low[x]!=dfn[x]);       }}int judge(int N){       for (int i=0;i<N;i++)          if (tp[i<<1]==tp[i<<1|1]) return 0;       return 1;}int main(){            int N,M;        while (~scanf("%d%d",&N,&M))       {               Lnum=0;               memset(_next,0,sizeof(_next));               while (M--)               {                       char c1,c2;                       int x,y;                       do { c1=getchar(); }while (c1!='+' && c1!='-');                       scanf("%d",&x);                       do { c2=getchar(); }while (c2!='+' && c2!='-');                       scanf("%d",&y);                       if (c1=='-' && c2=='-') addline(x<<1|1,y<<1),addline(y<<1|1,x<<1);                       if (c1=='-' && c2=='+') addline(x<<1|1,y<<1|1),addline(y<<1,x<<1);                       if (c1=='+' && c2=='-') addline(x<<1,y<<1),addline(y<<1|1,x<<1|1);                       if (c1=='+' && c2=='+') addline(x<<1,y<<1|1),addline(y<<1,x<<1|1);               }               memset(dfn,0,sizeof(dfn));               memset(instack,false,sizeof(instack));               while (!mystack.empty()) mystack.pop();               DfsIndex=tpnum=0;               for (int i=0;i<(N<<1);i++)                  if (!dfn[i]) tarjan(i);               printf("%d\n",judge(N));       }       return 0;} 


原创粉丝点击