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
发起支付 接入支付宝 支付的大致流程如下图: 部分节点详解: 沙箱环境 是支付宝提供给开发者的 ...
随机推荐
- O - 覆盖的面积 - hdu 1255(求面积)
分析:求一层的面积覆盖是非常简单的事情,不过多层面积覆盖应该怎么搞???也是比较简单的事情,只需要用两个变量记录就好了,一个记录覆盖一次的,一个记录覆盖两次的,就很容易解决了 ************ ...
- 警告: 隐式声明与内建函数‘exit’不兼容 [默认启用]
警告: 隐式声明与内建函数‘exit’不兼容 [默认启用] 最近在学习linux下的多任务编程,用到exit等函数时,经常出现该警告,查找资料后发现,原因其实很简单,没有把stdlib.h头文件包含进 ...
- nginx 配置自签名的ssl证书
最近要搭一个https的测试环境,使用nginx做反向代理. 网上找过不少资料,但过程不是很完整,吃了不少亏,故把自己的操作过程总结下来.如果你刚好遇到这个问题,希望对你有帮助! ********** ...
- hdu2209翻纸牌游戏
翻纸牌游戏 Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- 一小时搞定DIV+CSS布局-固定页面开度布局
本文讲解使用DIV+CSS布局最基本的内容,读完本文你讲会使用DIV+CSS进行简单的页面布局. 转载请标明:http://www.kwstu.com/ArticleView/divcss_20139 ...
- (转)Newtonsoft.Json序列化和反序列
这里下载:http://www.newtonsoft.com/products/json/安装: 1.解压下载文件,得到Newtonsoft.Json.dll 2.在项目中添加引用.. 序列化 ...
- iframe顶部跳转跨域问题
$("#button").on("click", function () { // top.location.locatio ...
- Android material design support library -- CollapsingToolbarLayout简介
本文是codetrick上material design support library教程的第三篇,主要讲的是CollapsingToolbarLayout的概念和应用方法. 原文链接:Materi ...
- c - 比较字符串的大小
c的标准库中当然有现成的比较字符串的函数<string.h>中的 strcmp int __cdecl strcmp(_In_z_ const char * _Str1, _In_z_ c ...
- HibernateTemplate常用方法总结
HibernateTemplate常用方法 (本文章内容相当于转载自:http://www.tuicool.com/articles/fU7FV3,只是整理了一下内容结构和修改了部分内容,方便阅读) ...