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;}
- POJ 3905 - Perfect Election 简单2-sat
- POJ 3905 Perfect Election(简单2-SAT)
- poj 3905 Perfect Election(2-SAT判断简单)
- poj 3905 Perfect Election 【2-sat 简单建图】
- 【POJ】3905 Perfect Election 2-sat
- POJ 3905 Perfect Election(2-sat)
- POJ 3905 Perfect Election(2-sat)
- poj 3905 Perfect Election 2-sat
- POJ - 3905 Perfect Election(2-SAT)
- POJ 3905 Perfect Election(2-SAT)
- poj--3905--Perfect Election(2-sat)
- Poj 3905 Perfect Election(2-sat 可行性判定)
- POJ 3905 Perfect Election (初学2-Sat)
- 2-SAT:以 POJ 3905 - Perfect Election 为例
- Poj 3905 Perfect Election【2-SAT-------Tarjan强连通】
- POJ 3905Perfect Election 2-sat判断可行解
- poj3905 Perfect Election (2-SAT)
- POJ 3905 Perfect Election
- GPU高性能计算与图像处理
- tr有一个单击事件,其中有个td有一个checkbox,当我单击checkbox时不触发tr的事件,注意tr的单击事件是JavaScript写的且带了参数!
- 4G(LTE)背后的技术和利益纠结——VoIP,VoLTE,SIP,IMS的前世今生
- ajax json 显示数据
- jquery 的ajax 应用
- POJ 3905 - Perfect Election 简单2-sat
- Linux下,Weblogic开机启动
- Dev c++ No rule to make target问题解决
- 小Q系列故事——最佳裁判
- SQL语句大全
- C/C++中系统时间的实现
- 3.2 Fragment的生命周期
- 大数据分析架构中需要权衡的四个因素
- 解决在eclipse中导入项目名称已存在的问题