poj2723-Get Luffy Out
一道2-SAT问题,每对钥匙需要加一条边,每扇门上的对应的要用的钥匙加一条边。
其实求解2-SAT问题,关键在于找到不能同时成立的条件,例如在本题中,每对钥匙不能同时使用,每扇门上的钥匙不能同时不使用。
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std; const int maxn = <<; struct TwoSAT
{
int n;
vector<int> G[maxn*];
bool mark[maxn*];
int S[maxn*], c; bool dfs(int x)
{
if (mark[x^]) return false;
if (mark[x]) return true;
mark[x] = true;
S[c++] = x;
for (int i = ; i < G[x].size(); i++)
if (!dfs(G[x][i])) return false;
return true;
} void init(int n)
{
this->n = n;
for (int i = ; i < n*; i++) G[i].clear();
memset(mark, , sizeof(mark));
} // x = xval or y = yval
void add_clause(int x, int xval, int y, int yval)
{
x = x * + xval;
y = y * + yval;
G[x^].push_back(y);
G[y^].push_back(x);
} bool solve()
{
for(int i = ; i < n*; i += )
if(!mark[i] && !mark[i+])
{
c = ;
if(!dfs(i))
{
while(c > ) mark[S[--c]] = false;
if(!dfs(i+)) return false;
}
}
return true;
}
}; ///////////////////////////////////////////////////////////////
#include <iostream>
TwoSAT solver;
typedef pair<int ,int > CPair;
#define K1 first
#define K2 second
CPair doors[<<];
CPair keys[<<];
int n; bool test(int nd)
{
solver.init(*n);
// 重新构图
// 加入钥匙
for(int i=;i<n;i++)
{
solver.add_clause(keys[i].K1,,keys[i].K2,); // 1表示使用key,0表示不使用
}
// 加入门
for(int i=;i<=nd;i++)
{
solver.add_clause(doors[i].K1,,doors[i].K2,); // 1表示使用key,0表示不使用
}
return solver.solve();
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif int nDoor;
while(scanf("%d %d",&n,&nDoor),n+nDoor)
{
// 添加一对钥匙
for(int i=;i<n;i++)
{
scanf("%d %d",&keys[i].K1,&keys[i].K2);
} // 门
for(int i=;i<nDoor;i++)
{
scanf("%d %d",&doors[i].K1,&doors[i].K2);
} int L=,R=nDoor-;
while(L < R)
{
int M = L + (R-L+)/;
if(test(M)) L = M;
else R = M-;
}
printf("%d\n",L+);
} return ;
}
poj2723-Get Luffy Out的更多相关文章
- POJ2723 Get Luffy Out 【2-sat】
题目 Ratish is a young man who always dreams of being a hero. One day his friend Luffy was caught by P ...
- hdu3715 Go Deeper[二分+2-SAT]/poj2723 Get Luffy Out[二分+2-SAT]
这题转化一下题意就是给一堆形如$a_i + a_j \ne c\quad (a_i\in [0,1],c\in [0,2])$的限制,问从开头开始最多到哪条限制全是有解的. 那么,首先有可二分性,所以 ...
- POJ2723 Get Luffy Out解题报告tarjan+2-SAT+二分
今天看到讲2-SAT比较好的blog,感觉微微的理解了2-SAT 传送门 参考: https://blog.csdn.net/leolin_/article/details/6680144 题意:你有 ...
- 【POJ2723】Get Luffy Out - 二分+2-SAT
题面描述 Ratish is a young man who always dreams of being a hero. One day his friend Luffy was caught by ...
- Poj2723:Get Luffy Out
题意 给出 n 对钥匙,每对只能挑一把使用,每把只能用一次,当一对钥匙中的一把被使用后,另一把也就不能再用了:然后给出 m 道门,每个门都有两把钥匙可以打开,问最多能开几道门(按给出的顺序开). So ...
- 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 ...
- luffy项目后台drf搭建(1)
一 进入虚拟环境 打开crm,输入命令 workon luffy 虚拟环境使用文档 二 安装基本类库 pip install django pip install PymySQL pip instal ...
- 项目部署Vue+Django(luffy)
部署路飞学城 部署整体框架图: 1 熟悉linux操作 2 上传路飞学城项目到linux服务器 xftp上传到服务器 lrzsz工具 3 完成python3解释器的安装 在linux命令行模式下, 输 ...
- Luffy之课程详情页
Luffy之课程详情页 提前写好课程详情的template,并放入到Vue中 注册路由 import CourseDetail from "../components/CourseDetai ...
- Luffy之支付宝支付开发API
发起支付 接入支付宝 支付的大致流程如下图: 部分节点详解: 沙箱环境 是支付宝提供给开发者的 ...
随机推荐
- 兼容ie6/ff/ch/op的div+css实现的圆角框
<!DOCTYPE html> <html> <head> <title>青春不迷茫:寻梦时代的“蚁族”逆袭之旅- 职场管理专题-中国人力资源开发网-中 ...
- 深度分析Linux下双网卡绑定七种模式
现在一般的企业都会使用双网卡接入,这样既能添加网络带宽,同时又能做相应的冗余,可以说是好处多多.而一般企业都会使用linux操作系统下自带的网卡绑定模式,当然现在网卡产商也会出一些针对windows操 ...
- JS-Array数组内置对象
直接上代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...
- 从一些简单代码实例彻底理解面向对象编程思想|OOP本质是什么?
从Rob Pike 的 Google+上的一个推看到了一篇叫<Understanding Object Oriented Programming>的文章,我先把这篇文章简述一下,然后再说说 ...
- 一种调用opencv库的C++工程通用的Makefile模板
第一次自己写makefile,记录一下 #Compilers #CXX=/opt/compiler/gcc-/bin/g++ CXX = g++ #Includes INCLUDE_FLAGS = - ...
- (转)鸟哥SHELL入门材料
http://blog.chinaunix.net/space.php?uid=9809038&do=blog&cuid=62903 经典入门材料! 学习 Shell Scripts ...
- web性能优化——JSP
一.啰嗦 做web开发的都知道,性能的重要性就不必强调了.就前端展示的工作来说,jsp大家都熟悉html更熟悉:web服务器tomcat应该是最熟悉的了:web方面的基础知识上来说,静态页面比动态页面 ...
- Accordion( 分类) 组件
一. 加载方式 //class 加载方式<div id="box" class="easyui-accordion"style="width:3 ...
- 前端bug记录---不定时更新
在项目的开发中难免遇到各种各样的bug,我觉得还是有必要记录一下的,方便日后查询. safari window resize 为满足日常轮播需求,做一个符合当前业务的轮播插件,其中需要考虑windo ...
- arcgis server manager - An error has occured on the server. For details please check the Event (Application) log on the web.
当登陆 Arcgis Server Manager的时候,点击 "Services" 中的选项"Manage Services",就报错: An error h ...