POJ2723-Get Luffy Out(2-SAT)
题意:有m扇门,每个门上有两把锁,打开任意一个锁都可以打开这扇门。门要按顺序一个一个打开。
现在有n对不同的钥匙,每对钥匙只能用其中一个,问最多能打开多少门。
题解:对钥匙建图,门是限制条件来建边。每加一扇门就多一个限制条件,直到2-sat不满足为止。当然二分会更快一些。有一个trick就是门上的两把锁相同的情况,也是就这个钥匙是必须用的,建边特殊考虑。
PS:我到底要错多少次才能长记性数组开足够大!!!以后WA了看数组!!TLE了看数组!!RE了看数组!!!
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std; typedef long long ll; const int N = <<;
const int M = <<; struct Edge {
int from, to, next;
} edge[M];
int head[N];
int cntE;
void addedge(int u, int v) {
edge[cntE].from = u; edge[cntE].to = v; edge[cntE].next = head[u]; head[u] = cntE++;
} int dfn[N], low[N], idx;
int stk[N], top;
int in[N];
int kind[N], cnt; void tarjan(int u)
{
dfn[u] = low[u] = ++idx;
in[u] = true;
stk[++top] = u;
for (int i = head[u]; i != -; i = edge[i].next) {
int v = edge[i].to;
if (!dfn[v]) tarjan(v), low[u] = min(low[u], low[v]);
else if (in[v]) low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u]) {
++cnt;
while () {
int v = stk[top--]; kind[v] = cnt; in[v] = false;
if (v == u) break;
}
}
} bool sat(int n) // 序号从0开始
{
for (int i = ; i < *n; ++i) if (!dfn[i]) tarjan(i);
for (int i = ; i < *n; i += ) {
if (kind[i] == kind[i^]) return false;
}
return true;
} void init() {
memset(dfn, , sizeof dfn);
memset(in, false, sizeof in);
idx = top = cnt = ;
}
int key[M];
int a[N], b[N];
int main()
{
int n, m;
int u, v;
while (~scanf("%d%d", &n, &m) && n) {
cntE = ; memset(head, -, sizeof head);
for (int i = ; i < n; ++i) {
scanf("%d%d", &u, &v);
key[u] = *i; key[v] = *i+;
} for (int i = ; i < m; ++i) {
scanf("%d%d", a+i, b+i);
}
for (int i = ; i < m; ++i) {
if (a[i] == b[i]) addedge(key[ a[i] ]^, key[ a[i] ]);
else {
addedge(key[ a[i] ]^, key[ b[i] ]);
addedge(key[ b[i] ]^, key[ a[i] ]);
}
init();
if (!sat(n)) {
printf("%d\n", i);
break;
}
if (i == m-) {
printf("%d\n", m);
}
}
} return ;
} /**
3 3
1 2 3 4 5 6
3 3 2 2 4 4 3 3
0 1 2 3 4 5
0 2 1 1 3 3 **/
POJ2723-Get Luffy Out(2-SAT)的更多相关文章
- 学习笔记(two sat)
关于two sat算法 两篇很好的论文由对称性解2-SAT问题(伍昱), 赵爽 2-sat解法浅析(pdf). 一些题目的题解 poj 3207 poj 3678 poj 3683 poj 3648 ...
- 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 ...
- Katu Puzzle POJ - 3678 (2 - sat)
有N个变量X1X1~XNXN,每个变量的可能取值为0或1. 给定M个算式,每个算式形如 XaopXb=cXaopXb=c,其中 a,b 是变量编号,c 是数字0或1,op 是 and,or,xor 三 ...
- LA 3211 飞机调度(2—SAT)
https://vjudge.net/problem/UVALive-3211 题意: 有n架飞机需要着陆,每架飞机都可以选择“早着陆”和“晚着陆”两种方式之一,且必须选择一种,第i架飞机的早着陆时间 ...
- spring定时任务详解(@Scheduled注解)( 转 李秀才的博客 )
在springMVC里使用spring的定时任务非常的简单,如下: (一)在xml里加入task的命名空间 xmlns:task="http://www.springframework.or ...
- MongoDB 聚合管道(Aggregation Pipeline)
管道概念 POSIX多线程的使用方式中, 有一种很重要的方式-----流水线(亦称为"管道")方式,"数据元素"流串行地被一组线程按顺序执行.它的使用架构可参考 ...
- mysql触发器,答题记录表同步教学跟踪(用户列表)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABVQAAAOOCAIAAABgEw4AAAAgAElEQVR4nOy92VcT27r/zX+xLtflvt
- Linux版Matlab R2015b的bug——脚本运行的陷阱(未解决)
0 系统+软件版本 系统:CentOS 6.7 x64, 内核 2.6.32-573.el6.x86_64软件:Matlab R2015b(包括威锋网和东北大学ipv6下载的资源,都测试过) 1 脚本 ...
- Quartz.net(调度框架) 使用Mysql作为存储
最近公司的做的项目中涉及到配置任务地址然后按照配置去目标地址提取相关的数据,所以今天上午在Internet上查看有关定时任务(调度任务)的相关信息,筛选半天然后查找到Quartz.net. Quart ...
- mysql 函数编程大全(持续更新)
insert ignore insert ignore表示,如果中已经存在相同的记录,则忽略当前新数据 如果您使用一个例如“SET col_name = col_name + 1”的赋值,则对位于右侧 ...
随机推荐
- [数论]ZOJ3593 One Person Game
题意:一个人要从A走到B 只能走a布.b步.(a+b)步,可以往左或右走 问 最少要走几步,不能走到的输出-1 可以列出方程 $ax+by=A-B$ 或者 $ax+(a+b)y=A-B$ 或者 ...
- 李洪强iOS开发之OC[018]对象和方法之间的关系
// // main.m // 18 - 对象和方法之间的关系 // // Created by vic fan on 16/7/14. // Copyright © 2016年 李洪强. A ...
- Python高级特性
比如构造一个1, 3, 5, 7, ..., 99的列表,可以通过循环实现: 这是正确的姿势
- POJ3259——Wormholes(Bellman-Ford+SPFA)
Wormholes DescriptionWhile exploring his many farms, Farmer John has discovered a number of amazing ...
- Tableau
http://tableau.analyticservice.net/desktop.html
- DataTable ,XML和JSON相互转化
网上搜集的,保留下来了,有需要的拿去!private string DataTable2Xml(DataTable dt) { StringBuilder xmlBuilder = new Strin ...
- linux的终端,网络虚拟终端,伪终端(转)
blog.csdn.net/todd911/article/details/8025540 Linux上许多网络服务应用,如l2tp.pptp.telnet,都用到了伪终端.有朋友在问这方面的概念 ...
- 不要在头文件中使用 using namespace std;
不要在头文件中使用(using namespace std;). 若你使用了using namespace std;,在某一头文件中,那么包含这些头文件的文件就失去了"namespace ...
- java文档注释主要使用方法
一.java包含哪些注释 1.//用于单行注释. 2./*...*/用于多行注释,从/*开始,到*/结束,不能嵌套. 3./**...*/则是为支持jdk工具javadoc.exe而特有的注释语句.这 ...
- Eclipse搭建Struts框架,及一个简单的Struts例子
一.下载struts2.0.1 http://struts.apache.org/downloads.html,下载struts-2.0.1-all.zip,这个压缩包中包含了开发struts2所需的 ...