两个钥匙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的更多相关文章

  1. HDU 1816, POJ 2723 Get Luffy Out(2-sat)

    HDU 1816, POJ 2723 Get Luffy Out pid=1816" target="_blank" style="">题目链接 ...

  2. POJ 2723 Get Luffy Out(2-SAT+二分答案)

    Get Luffy Out Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8851   Accepted: 3441 Des ...

  3. 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 ...

  4. 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 ...

  5. poj 2723 Get Luffy Out 二分+2-sat

    题目链接 给n个钥匙对, 每个钥匙对里有两个钥匙, 并且只能选择一个. 有m扇门, 每个门上有两个锁, 只要打开其中一个就可以通往下一扇门. 问你最多可以打开多少个门. 对于每个钥匙对, 如果选择了其 ...

  6. Get Luffy Out (poj 2723 二分+2-SAT)

    Language: Default Get Luffy Out Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7969   ...

  7. POJ 2723 HDU 1816 Get Luffy Out

    二分答案 + 2-SAT验证 #include<cstdio> #include<cstring> #include<cmath> #include<stac ...

  8. poj 2723

    Get Luffy Out Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7295   Accepted: 2778 Des ...

  9. 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 ...

随机推荐

  1. c++ 开放随笔

    1.设计或使用类时 一定先弄清流程 了解对象直接的关系(这个必须清楚) 如多对多,一对多,一对一关系.不然弄list map set时相对麻烦. 2.map 中key是不能重复的,可以看成一个主键,定 ...

  2. SQLite学习手册

    在实际的应用中,SQLite作为目前最为流行的开源嵌入式关系型数据库,在系统的架构设计中正在扮演着越来越为重要的角色.和很多其它嵌入式NoSQL数据库不同的是,SQLite支持很多关系型数据库的基本特 ...

  3. fireworks切图

    下载安装完成后打开软件 打开一张图片 首选参数的撤销次数改成999 按住空格键 鼠标会变成小手的形状 这时候可以拖拽图像 找到切片工具 记住缩放比例的快捷键 ctrl+空格 放大某个区域 切的时候按住 ...

  4. when an event of selector will be fired

    OP_READ Operation-set bit for read operations. Suppose that a selection key's interest set contains ...

  5. ubuntu zip解压

    您好,zip xx.zip压缩,unzip xx.zip 解压,tar zcvf xx.tar.gz压缩tar zxvf xx.tar.gz解压

  6. ios开发之--ios11适配:TableView的heightForHeaderInSection设置高度无效/UISearchBar消失

    更新到ios11,然后使用x-code9运行项目,发现tableview的-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInS ...

  7. 如何连接 MySQL

    操作如下: [root@localhost ~]$ mysql -uroot -p' # 本地连接 MySQL 服务 [root@localhost ~]$ mysql -uroot -p' -h19 ...

  8. Thinkphp 图形验证码无法显示

    不显示验证码的代码: public function verify(){ $verify = new \Think\Verify(); $verify->entry(); } 修改为: publ ...

  9. postgres模板数据库

    CREATE DATABASE 实际上是通过拷贝一个现有的数据库进行工作的.缺省时,它拷贝名为 template1 的标准系统数据库.所以该数据库是创建新数据库的"模板".如果你给 ...

  10. java高级---->Thread之CyclicBarrier的使用

    CyclicBarrier是一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).今天我们就学习一下CyclicBarrier的用法. Cycl ...