poj 2723 Get Luffy Out 2-SAT
两个钥匙a,b是一对,隐含矛盾a->!b。b->!a
一个门上的两个钥匙a,b,隐含矛盾!a->b,!b->a(看数据不大,我是直接枚举水的,要打开当前门,没选a的话就一定要选b打开。没选b的话,就一定要选a打开)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn 2222
vector<int> g[maxn*2];
bool mark[maxn*2];
int S[maxn*2],cnt,n,m;
int ys[maxn*2];
void init()
{
for(int i=0;i<n*2*2;i++) g[i].clear();
memset(mark,false,sizeof(mark));
}
void add(int x,int xval,int y,int yval)
{
x=x*2+xval;
y=y*2+yval;
g[x].push_back(y);
}
bool dfs(int x)
{
if(mark[x^1]) return false;
if(mark[x]) return true;
mark[x]=true;
S[cnt++]=x;
for(int i=0;i<g[x].size();i++) if(!dfs(g[x][i])) return false;
return true;
}
bool solve()
{
for(int i=0;i<n*2;i+=2)
{
if(!mark[i]&&!mark[i+1])
{
cnt=0;
if(!dfs(i))
{
while(cnt>0) mark[S[--cnt]]=false;
if(!dfs(i+1)) return false;
}
}
}
return true;
}
int main()
{
int a,b,c;
while(~scanf("%d%d",&n,&m))
{
if(!n&&!m) break;
init();
for(int i=0;i<n;i++)
{
scanf("%d%d",&a,&b);
add(a,1,b,0);
add(b,1,a,0);
}
n*=2;
int flag=0;
int ans=0;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
if(flag) continue;
add(a,0,b,1);
add(b,0,a,1);
memset(mark,0,sizeof(mark));
if(solve()) ans=i;
else flag=1;
}
printf("%d\n",ans);
}
return 0;
}
poj 2723 Get Luffy Out 2-SAT的更多相关文章
- HDU 1816, POJ 2723 Get Luffy Out(2-sat)
HDU 1816, POJ 2723 Get Luffy Out pid=1816" target="_blank" style="">题目链接 ...
- POJ 2723 Get Luffy Out(2-SAT+二分答案)
Get Luffy Out Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8851 Accepted: 3441 Des ...
- poj 2723 Get Luffy Out(2-sat)
Description Ratish is a young man who always dreams of being a hero. One day his friend Luffy was ca ...
- poj 2723 Get Luffy Out-2-sat问题
Description Ratish is a young man who always dreams of being a hero. One day his friend Luffy was ca ...
- poj 2723 Get Luffy Out 二分+2-sat
题目链接 给n个钥匙对, 每个钥匙对里有两个钥匙, 并且只能选择一个. 有m扇门, 每个门上有两个锁, 只要打开其中一个就可以通往下一扇门. 问你最多可以打开多少个门. 对于每个钥匙对, 如果选择了其 ...
- Get Luffy Out (poj 2723 二分+2-SAT)
Language: Default Get Luffy Out Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7969 ...
- POJ 2723 HDU 1816 Get Luffy Out
二分答案 + 2-SAT验证 #include<cstdio> #include<cstring> #include<cmath> #include<stac ...
- poj 2723
Get Luffy Out Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7295 Accepted: 2778 Des ...
- POJ 3678 Katu Puzzle(2 - SAT) - from lanshui_Yang
Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...
随机推荐
- zookper3.4.6集群配置
参考链接: http://blog.csdn.net/shirdrn/article/details/7183503 个人感觉zookeeper 安装在单机上无操作意义,所以直接记录集群配置过程. 连 ...
- wamp下修改mysql root用户的登录密码方法
wamp环境安装之后mysql的root密码为空的,我们希望给它设置一个密码; 1.安装好wamp后,运行WampServer程序,进入MYSQL控制台: 2.进入控制台后,提示输入密码(不用输入任何 ...
- JavaScript------去掉Array中重复值
转载: http://blog.csdn.net/teresa502/article/details/7926796 代码: // 删除数组中重复数据 function removeDuplElem( ...
- [转载]2014年10月26完美世界校招两道java题
public class VolitileTest { volatile static int count=0; public static void main(String args[]){ for ...
- 安装memcacheq
1.下载memcacheq包 下载地址:http://code.google.com/p/memcacheq/downloads/list 解压包:# tar -zxvf memcache ...
- Hadoop学习之路
Hadoop是谷歌的集群系统的开源实现: -google集群系统:GFS.MapReduce.BigTable -Hadoop主要由HDFS(hadoop distrubuted file syste ...
- 上传文件提示IO Error
百度查到的解决办法 http://www.wang0214.com/news/466.html 作者:深圳网站建设 原因: Asp.net中,上传文件的默认大小是4096 KB,也就是4M,不过你可以 ...
- AVL 平衡树
AVL是一种平衡二叉树,它通过对二叉搜索树中的节点进行旋转使得二叉搜索树达到平衡.AVL在所有的平衡二叉搜索树中具有最高的平衡性. 定义 平衡二叉树或者为空树或者为满足如下性质的二叉搜索树: 左右子树 ...
- LeetCode - Delete Duplicate Emails
Discription:Write a SQL query to delete all duplicate email entries in a table named Person, keeping ...
- 【Android N 7.1.1】 处于锁屏界面时可以转屏
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarWindowManager.ja ...