POJ 3905 Perfect Election(2-SAT)
来源:互联网 发布:7.0采集助手数据库 编辑:程序博客网 时间:2024/05/16 18:47
Description
一个城市要选举,有n位竞选者参与竞选(标号1~n),有m为评委,每位评委对于某种结果会比较开心,有四种情况
+i +j:i,j至少一个被选上
-i -j:i,j至少一个没被选上
+i -j:i被选上或者j没被选上或者两者均发生
-i +j : i没被选上或者j被选上或者两者均发生
问是否存在一种方案使得所有评委都很开心,有输出1,没有输出0
Input
多组用例,每组用例占一行,首先输入两个整数n和m表示竞选者数量和评委数量,之后输入每个评委的投票,以文件尾结束输入
Output
如果存在使得所有评委都开心的选举结果则输出1,否则输出0
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
Solution
2-SAT,建图规则如下
+i +j:add(i+n,j),add(j+n,i)
-i -j:add(i,j+n),add(j,i+n)
+i -j:add(j,i),add(i+n,j+n)
-i +j:add(j+n,i+n),add(i,j)
按照以上规则建好图后用tarjan算法求强连通分量并判断是否存在使得布尔公式值为真的一组布尔变量赋值即可
Code
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<queue>#include<stack>#include<map>#include<vector>#include<string>using namespace std;#define maxn 4444#define maxm 4444444stack<int>st; int n,m,scc,index; int low[maxn],dfn[maxn],instack[maxn],fa[maxn]; struct edge{ int to,next;};edge g[maxm];int head[maxm],tol;void init()//初始化 { tol=0; memset(head,-1,sizeof(head)); scc=index=0; while(!st.empty())st.pop(); memset(dfn,0,sizeof(dfn)); memset(instack,0,sizeof(instack)); memset(low,0,sizeof(low));} void add(int a,int b)//建边 { g[tol].to=b; g[tol].next=head[a]; head[a]=tol++; }void tarjan(int u)//求强联通分量 { dfn[u]=low[u]=++index; instack[u]=1; st.push(u); int v; for(int i=head[u];i!=-1;i=g[i].next) { v=g[i].to; if(!dfn[v]) { tarjan(v); low[u]=min(low[u],low[v]); } else if(instack[v]) low[u]=min(low[u],dfn[v]); } if(dfn[u]==low[u]) { scc++; do { v=st.top(); st.pop(); fa[v]=scc; instack[v]=0; }while(v!=u); } } void solve(){ for(int i=1;i<=2*n;i++) if(!dfn[i]) tarjan(i); } int main(){ while(~scanf("%d%d",&n,&m)) { init(); char s1[5],s2[5]; while(m--) { scanf("%s%s",s1,s2); int a=0,b=0; for(int i=1;s1[i]>='0'&&s1[i]<='9';i++) a=a*10+s1[i]-'0'; for(int i=1;s2[i]>='0'&&s2[i]<='9';i++) b=b*10+s2[i]-'0'; //建图 if(s1[0]=='+'&&s2[0]=='+') add(a+n,b),add(b+n,a); if(s1[0]=='-'&&s2[0]=='-') add(a,b+n),add(b,a+n); if(s1[0]=='+'&&s2[0]=='-') add(a+n,b+n),add(b,a); if(s1[0]=='-'&&s2[0]=='+') add(a,b),add(b+n,a+n); } solve();//tarjan int flag=1; for(int i=1;i<=n;i++) if(fa[i]==fa[i+n])//存在矛盾 { flag=0; break; } printf("%d\n",flag); } return 0;}
0 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判断可行解
- POJ 3905 Perfect Election(2-sat 模板题,3级)
- poj3905 Perfect Election (2-SAT)
- 使用脚本获得 消耗Linux操作系统内存的前10个用户
- 条款7中提及工厂method
- Regular Expression Matching
- 软考路之线性表
- linux 编写自己的shell
- POJ 3905 Perfect Election(2-SAT)
- 海量数据挖掘MMDS week2: Nearest-Neighbor Learning最近邻学习
- Asphalting Roads
- CodeForces 441C Valera and Tubes
- OpenCV 学习(像素操作 Manipuating the Pixels)
- UIApplication和delegate以及程序启动过程
- 海量数据挖掘MMDS week2: Association Rules关联规则与频繁项集挖掘
- 证券基础--发行体制
- html小需求——科技文章展示