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

Description

Ratish is a young man who always dreams of being a hero. One day his friend Luffy was caught by Pirate Arlong. Ratish set off at once to Arlong's island. When he got there, he found the secret place where his friend was kept, but he could not go straight in. He saw a large door in front of him and two locks in the door. Beside the large door, he found a strange rock, on which there were some odd words. The sentences were encrypted. But that was easy for Ratish, an amateur cryptographer. After decrypting all the sentences, Ratish knew the following facts:

Behind the large door, there is a nesting prison, which consists of M floors. Each floor except the deepest one has a door leading to the next floor, and there are two locks in each of these doors. Ratish can pass through a door if he opens either of the two locks in it. There are 2N different types of locks in all. The same type of locks may appear in different doors, and a door may have two locks of the same type. There is only one key that can unlock one type of lock, so there are 2N keys for all the 2N types of locks. These 2N keys were divided into N pairs, and once one key in a pair is used, the other key will disappear and never show up again.

Later, Ratish found N pairs of keys under the rock and a piece of paper recording exactly what kinds of locks are in the M doors. But Ratish doesn't know which floor Luffy is held, so he has to open as many doors as possible. Can you help him to choose N keys to open the maximum number of doors?

Input

There are several test cases. Every test case starts with a line containing two positive integers N (1 <= N <= 210) and M (1 <= M <= 211) separated by a space, the first integer represents the number of types of keys and the second integer represents the number of doors. The 2N keys are numbered 0, 1, 2, ..., 2N - 1. Each of the following N lines contains two different integers, which are the numbers of two keys in a pair. After that, each of the following M lines contains two integers, which are the numbers of two keys corresponding to the two locks in a door. You should note that the doors are given in the same order that Ratish will meet. A test case with N = M = 0 ends the input, and should not be processed.

Output

For each test case, output one line containing an integer, which is the maximum number of doors Ratish can open.

Sample Input

3 6
0 3
1 2
4 5
0 1
0 2
4 1
4 2
3 5
2 2
0 0

Sample Output

4

题目链接:POJ 2723

用2-SAT来check的二分答案的一道题目,由于题目的门是从上到下顺序进入的,因此才能二分这个位置,然后检测第1道门~第mid道门是否均能通过。

那么就简单了,首先把钥匙拆成两个点,用和不用,然后一共有2N个钥匙,那么就有4N个点,然后每次建图肯定是先把钥匙的矛盾点建好,然后再用1~mid的门的信息再加入一些边,然后check方案是否存在即可,一开始想不出来怎么做后来发现是按输入的顺序来开门的,然后如果一个钥匙pair为 a与b,如果你选了a,那么b会消失,但a可以一直用,不会消失,感觉以前大一的时候也在某一个网络赛里见过这个题,意思基本相同就题面变一下,可惜当时并不懂2-SAT……写了个暴力dfs结果T了……惨

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = (1 << 10) + 10;
const int MAXV = N << 2;
const int MAXE = N * 6;
struct edge
{
int to, nxt;
edge() {}
edge(int _to, int _nxt): to(_to), nxt(_nxt) {}
};
edge E[MAXE];
int head[MAXV], tot;
int vis[MAXV], st[MAXV], top;
int n, m;
int door[N << 1][2], key[N][2]; int rev(int k)
{
return k < (n << 1) ? k + (n << 1) : k - (n << 1);
}
inline void add(int s, int t)
{
E[tot] = edge(t, head[s]);
head[s] = tot++;
}
void init()
{
CLR(head, -1);
tot = 0;
CLR(vis, 0);
}
int dfs(int u)
{
if (vis[rev(u)])
return 0;
if (vis[u])
return 1;
vis[u] = 1;
st[top++] = u;
for (int i = head[u]; ~i; i = E[i].nxt)
{
int v = E[i].to;
if (!dfs(v))
return 0;
}
return 1;
}
int check()
{
for (int i = 0; i < (n << 2); ++i)
{
top = 0;
if (!vis[i] && !vis[rev(i)] && !dfs(i))
{
while (top)
vis[st[--top]] = 0;
if (!dfs(rev(i)))
return 0;
}
}
return 1;
}
void build(int last)
{
init();
for (int i = 0; i < n; ++i) //2n
{
add(key[i][0], rev(key[i][1]));
add(key[i][1], rev(key[i][0]));
}
for (int i = 1; i <= last; ++i) //2m
{
add(rev(door[i][0]), door[i][1]);
add(rev(door[i][1]), door[i][0]);
}
}
int main(void)
{
int i;
while (~scanf("%d%d", &n, &m) && (n || m))
{
init();
for (i = 0; i < n; ++i)
scanf("%d%d", &key[i][0], &key[i][1]);
for (i = 1; i <= m; ++i)
scanf("%d%d", &door[i][0], &door[i][1]);
int L = 0, R = m;
int ans = 0;
while (L <= R)
{
int mid = MID(L, R);
build(mid);
if (check())
{
ans = mid;
L = mid + 1;
}
else
R = mid - 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)

    Description Ratish is a young man who always dreams of being a hero. One day his friend Luffy was ca ...

  3. POJ 3294 Life Forms(后缀数组+二分答案)

    [题目链接] http://poj.org/problem?id=3294 [题目大意] 求出在至少在一半字符串中出现的最长子串. 如果有多个符合的答案,请按照字典序输出. [题解] 将所有的字符串通 ...

  4. POJ 3080 Blue Jeans(后缀数组+二分答案)

    [题目链接] http://poj.org/problem?id=3080 [题目大意] 求k个串的最长公共子串,如果存在多个则输出字典序最小,如果长度小于3则判断查找失败. [题解] 将所有字符串通 ...

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

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

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

  7. TTTTTTTTTTTTTTTT POJ 2723 楼层里救朋友 2-SAT+二分

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

  8. poj 2723 Get Luffy Out 2-SAT

    两个钥匙a,b是一对,隐含矛盾a->!b.b->!a 一个门上的两个钥匙a,b,隐含矛盾!a->b,!b->a(看数据不大,我是直接枚举水的,要打开当前门,没选a的话就一定要选 ...

  9. POJ 2758 Checking the Text(Hash+二分答案)

    [题目链接] http://poj.org/problem?id=2758 [题目大意] 给出一个字符串,支持两个操作,在任意位置插入一个字符串,或者查询两个位置往后的最长公共前缀,注意查询的时候是原 ...

随机推荐

  1. 【BZOJ2141】排队

    点此看题面 大致题意: 给你一个序列,每次交换两个数,求每次操作后的逆序对个数. 关于另一道题目 推荐先去看一下这道题目:[洛谷3759][TJOI2017] 不勤劳的图书管理员(貌似是此题的升级版) ...

  2. 关于C#的垃圾回收机制,Finalize和Dispose的区别(自认为很清晰了,有疑问的评论)

    来到个新地方,新学习C#,前面看到C#的垃圾回收,Finalize和Dispose时,总是一知半解,迷迷糊糊.这次好了,前面连续两次面试问到这个问题,脑子里不是很清晰,加上用英文来表达,更是雪上加霜的 ...

  3. DOS&8086微处理器

    DOS DOS环境,需要安装dosemu来模拟DOS环境(Ubuntu的应用商店就有),为了编写汇编,还需要DEBUG.MASM.LINK等汇编语言开发工具.如果你嫌麻烦,推荐使用实验楼已搭好的免费的 ...

  4. 什么是SAD,SAE,SATD,SSD,SSE,MAD,MAE,MSD,MSE?

    SAD(Sum of Absolute Difference)=SAE(Sum of Absolute Error)即绝对误差和 SATD(Sum of Absolute Transformed Di ...

  5. final关键字,static关键字

    Final final的意思为最终,不可变.final是个修饰符,它可以用来修饰类,类的成员,以及局部变量.不能修饰构造方法. 注意: 被final修饰的类不能被继承但可以继承别的类 class Yy ...

  6. UsbKey开发

    http://slf-1983.blog.163.com/blog/static/2990236320121113113955119/

  7. let和const在es6中的异同点

    let和const这两个都是声明一个变量或函数的方法与var差不太多的效果 let的声明在for循环中,当你定义的是多少,最后你的值就是多少开始的,它只进行一次循环,不会像var那样去一遍一遍的去遍历 ...

  8. Vue入门之v-if的使用

    在vue中一些常用的指令都是v-这样的,v-if是vue的一个内部指令,常用于html中 代码 <!DOCTYPE html> html lang="en"> & ...

  9. yum安装报错

    检查了好久才知道原来是 sudo nano /etc/sysconfig/network-scripts/ifcfg-ens33 下的DNS配错了,改好之后,sudo service network ...

  10. Linux时区修改

    Linux修改时区的正确方法 CentOS和Ubuntu的时区文件是/etc/localtime,但是在CentOS7以后localtime以及变成了一个链接文件 [root@centos7 ~]# ...