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 ...
随机推荐
- C++类中静态成员函数
引述自<深入探索C++对象模型>2001:5:1版次,p-150 static member functions的主要特性就是它没有this指针,所以: 1.它不能直接存取其所在class ...
- shell脚本程序中循环、判断语句的介绍
shell的循环主要有3种,for,while,until shell的分支判断主要有2种,if,case 一,for循环 C/C++ Code复制内容到剪贴板 #!/bin/bash for fil ...
- 动态提交使用jQuery 完成ajax 文件下载----后端php
1.js代码 // Ajax 文件下载 //当不用传参时,可以将data去掉 jQuery.download = function(url, data, method){ // 获得url和data ...
- Ubuntu创建新用户并增加管理员权限
1.Ubuntu中的root帐号默认是被禁用了的,所以登陆的时候没有这个账号 打开终端开启root账户 sudo passwd -u root sudo passwd root 设置root密码,输入 ...
- win8 学习笔记二 输出日志
Win8 Store App的日志输出不像Desktop App 那么简单,见 这篇文档(http://www.cnblogs.com/xiaokang088/archive/2011/12/27/2 ...
- 【Matlab】运动目标检测之“帧差法”
videoObj = VideoReader('4.avi');%读视频文件 nframes = get(videoObj, 'NumberOfFrames');%获取视频文件帧个数 : nframe ...
- OpenCV学习:改变图像的对比度和亮度
本实例演示简单地改变图像的对比度和亮度,使用了如下线性变换来实现像素值的遍历操作: The parameters α > 0 and β often called the gain and bi ...
- nginx配置技巧汇总
https://segmentfault.com/a/1190000000437323
- Emulator Error: Could not load OpenGLES emulation library: Could not load DLL!
Copy the file below from SDK\tools\lib to SDK\tools. libEGL_translator.dlllibGLES_CM_translator.dlll ...
- STM32学习之路之入门篇
2006年ARM公司推出了基于ARMV7架构的cortex系列的标准体系结构,以满足各种技术得不同性能要求,包含了A,R,M三个分工明确的系列 其中A系列面向复杂的尖端应用程序,用于运行开放式的复杂操 ...