首页 > 代码库 > POJ 2723 Get Luffy Out(图论-2SAT,搜索-二分)

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