POJ 3905 - Perfect Election 简单2-sat

21 篇文章 0 订阅

          题意:

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

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
                     问有没有一种安排问题的方法让每个人都快乐...

          题解:

                     很基础的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 long
using 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;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值