POJ 2723 Get Luffy Out(图论-2SAT,搜索-二分)

Get Luffy Out

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7488   Accepted: 2845

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

Source

Beijing 2005

题目大意:

有n对钥匙,m个门,每对钥匙用了其中1个,另一个就会消失,每个门上有m个锁,用特定的钥匙打开其中1个锁,另一个锁会消失,连续的打开门,问你之多能打开几扇门?

解题思路:

二分枚举打开的门数,再用2SAT判断是否矛盾。‘

2SAT构边说明:两边分别钥匙,为选与不选

(1)AB钥匙在一串,那么选了A钥匙,就不能选B钥匙;选了B钥匙,就不能选A钥匙

(2)AB是同一扇门的锁,那么不开A就要开B,不开B就要开A。

解题代码:

#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn=4100;

struct edge{
    int u,v,next;
    edge(int u0=0,int v0=0){
        u=u0,v=v0;
    }
}e[maxn*16];

int n,m,head[maxn],cnt;
int dfn[maxn],low[maxn],color[maxn],index,nc;
bool mark[maxn];
vector <edge> va,vb;
vector <int> vec;

void adde(int u,int v){
    e[cnt]=edge(u,v),e[cnt].next=head[u],head[u]=cnt++;
}

void init(){
    vec.clear();
    index=nc=cnt=0;
    for(int i=0;i<=4*n;i++){
        dfn[i]=0;
        color[i]=head[i]=-1;
        mark[i]=false;
    }
}

void input(){
    va.clear();
    vb.clear();
    va.resize(n);
    vb.resize(m);
    for(int i=0;i<n;i++) scanf("%d%d",&va[i].u,&va[i].v);
    for(int i=0;i<m;i++) scanf("%d%d",&vb[i].u,&vb[i].v);
}

void build(int r){
    init();
    for(int i=0;i<n;i++){
        adde(va[i].u,va[i].v+2*n);
        adde(va[i].v,va[i].u+2*n);
    }
    for(int i=0;i<=r;i++){
        adde(vb[i].u+2*n,vb[i].v);
        adde(vb[i].v+2*n,vb[i].u);
    }
}

void tarjan(int s){
    dfn[s]=low[s]=++index;
    mark[s]=true;
    vec.push_back(s);
    for(int i=head[s];i!=-1;i=e[i].next){
        int d=e[i].v;
        if(!dfn[d]){
            tarjan(d);
            low[s]=min(low[s],low[d]);
        }else if(mark[d]){
            low[s]=min(low[s],dfn[d]);
        }
    }
    if(low[s]==dfn[s]){
        int d;
        nc++;
        do{
            d=vec.back();
            vec.pop_back();
            color[d]=nc;
            mark[d]=false;
        }while(s!=d);
    }
}

bool judge(int r){
    build(r);
    for(int i=0;i<4*n;i++){
        if(!dfn[i]) tarjan(i);
    }
    for(int i=0;i<2*n;i++){
        if(color[i]==color[i+2*n]) return false;
    }
    return true;
}

void solve(){
    if(judge(m-1)){
        printf("%d\n",m);
        return;
    }
    int l=0,r=m-1;
    while(l<r){
        int mid=(l+r)/2;
        if(judge(mid)) l=mid+1;
        else r=mid;
    }
    printf("%d\n",r);
}

int main(){
    while(scanf("%d%d",&n,&m)!=EOF && (m||n) ){
        input();
        solve();
    }
    return 0;
}

POJ 2723 Get Luffy Out(图论-2SAT,搜索-二分)

时间: 2024-10-09 23:06:42

POJ 2723 Get Luffy Out(图论-2SAT,搜索-二分)的相关文章

HDU 1816, POJ 2723 Get Luffy Out(2-sat)

HDU 1816, POJ 2723 Get Luffy Out pid=1816" target="_blank" style="">题目链接 题意:N串钥匙.每串2把,仅仅能选一把.然后有n个大门,每一个门有两个锁,开了一个就能通过,问选一些钥匙,最多能通过多少个门 思路:二分通过个数.然后对于钥匙建边至少一个不选,门建边至少一个选,然后2-sat搞一下就可以. 一開始是按每串钥匙为1个结点,但是后面发现数据有可能一把钥匙,出如今不同串(真是不合

POJ 2723 Get Luffy Out(2-SAT)

[题目链接] http://poj.org/problem?id=2723 [题目大意] 给出一些钥匙和M扇有顺序的门,每扇门可以用两种钥匙打开, 每两把钥匙被绑在一起,绑在一起的钥匙只有其中一把可以使用, 问最多能按顺序打开几扇门. [题解] 因为门是按顺序的,因此能打开的门是单调, 首先我们二分这个答案,判定是否可行, 将二分得到的答案之前的门按两个钥匙孔拆分成两个点, 两个绑在一起的钥匙属于对立面,而门的两个钥匙孔是OR关系 A与B之间的OR关系,我们可以将其拆分为!A->B AND !B

poj 2723 Get Luffy Out 2-sat

题意: 有2*n把钥匙配成n对,每对中只能使用一把,另外有m道门,每道门能被2把药匙打开,问最多能从1开始按顺序打开多少道门. 分析: 二分枚举能打开的门数,用2-sat算法判断能否打开. 代码: //poj 2723 //sep9 #include <iostream> #include <cstdio> #include <string.h> #include <stack> using namespace std; const int maxN=100

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

HDU 3207 Ikki&#39;s Story IV - Panda&#39;s Trick(图论-2SAT,图论-tarjan)

Ikki's Story IV - Panda's Trick Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7821   Accepted: 2892 Description liympanda, one of Ikki's friend, likes playing games with Ikki. Today after minesweeping with Ikki and winning so many tim

poj 2723

Get Luffy Out Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7295   Accepted: 2778 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 Arlo

POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)

Secret Milking Machine Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9658   Accepted: 2859 Description Farmer John is constructing a new milking machine and wishes to keep it secret as long as possible. He has hidden in it deep within

POJ 3352 Road Construction(图论-tarjan)

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8647   Accepted: 4318 Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the ro

hdu1824-Let&#39;s go home:图论2-SAT

关键在于找出一定矛盾的条件,设一队的3个人为(a,b,c),a为队长,那么(a不留下,b不留下)矛盾,(a不留下,c不留下)矛盾; 对于每一对队员,(a留下,b留下)矛盾. 把模型建好,剩下的就是套模板了. 1 #include<cstdio> 2 #include<vector> 3 #include<cstring> 4 using namespace std; 5 6 const int maxn = 3000+10; 7 8 struct TwoSAT 9 {