POJ - 2723 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 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 <= 2 10) and M (1 <= M <= 2 11) 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.


题目分析

这里能开的门数量显然具有可二分性
而2-SAT又刚好起到判断作用
所以我们二分可以开的门的数量,再用2-SAT建图判定

一对钥匙只能用其中一把,我们可以利用这一点建图
设一扇门的两个锁分别为 a [ i ] , b [ i ]

设与 a [ i ] 一对的钥匙为 k e y [ a [ i ] ]
显然如果用了 k e y [ a [ i ] ] ,那么 a [ i ] 就不能再用
这扇门必须用 b [ i ] 解锁
所以连边 k e y [ a [ i ] ] b [ i ]

而一道门的两个锁 a [ i ] , b [ i ] 显然是的关系
所以还有连边 k e y [ b [ i ] ] a [ i ]
与上述同理


#include<iostream>
#include<stack>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long lt;

int read()
{
    int f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return f*x;
}

const int maxn=4010;
int n,m;
struct node{int v,nxt;}E[maxn<<2];
int head[maxn],tot;
int low[maxn],dfn[maxn],cnt;
int col[maxn],ins[maxn],colnum;
int key[maxn],a[maxn],b[maxn];
stack<int> st;
int ans;

void add(int u,int v)
{
    E[++tot].nxt=head[u];
    E[tot].v=v;
    head[u]=tot;
}

void init()
{
    tot=cnt=colnum=0;
    memset(head,0,sizeof(head));
    memset(dfn,0,sizeof(dfn));
    memset(col,0,sizeof(col));
    memset(low,0,sizeof(low));
}

void tarjan(int u)
{
    low[u]=dfn[u]=++cnt;
    st.push(u); ins[u]=1;
    for(int i=head[u];i;i=E[i].nxt)
    {
        int v=E[i].v;
        if(!dfn[v]){ tarjan(v); low[u]=min(low[u],low[v]);}
        else if(ins[v]) low[u]=min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u])
    {
        int v; colnum++;
        do
        {
            v=st.top();
            st.pop(); ins[v]=0;
            col[v]=colnum;
        }
        while(v!=u);
    }
}

int check(int x)
{
    init();
    for(int i=1;i<=x;++i)
    add(key[a[i]],b[i]),add(key[b[i]],a[i]);

    for(int i=1;i<=n<<1;++i)
    if(!dfn[i])tarjan(i);

    for(int i=1;i<=n<<1;++i)
    if(col[i]==col[key[i]])return 0;
    return 1;
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0)break;
        for(int i=1;i<=n;++i)
        {
            int x=read()+1,y=read()+1;
            key[x]=y; key[y]=x;
        }
        for(int i=1;i<=m;++i)
        a[i]=read()+1,b[i]=read()+1;

        int L=1,R=m,mid;
        while(L<R)
        {
            mid=L+R+1>>1;
            if(check(mid))L=mid;
            else R=mid-1;
        }
        printf("%d\n",L);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/niiick/article/details/81479320
今日推荐