poj 2723 Get Luffy Out(2-sat + 二分)

解决一道算法题目,通过选择钥匙开启尽可能多的门,利用深搜和二分查找优化解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Get Luffy Out
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6899 Accepted: 2631

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

题意:有n种钥匙,每种有两条(共有2*n条),当使用了某种钥匙中的其中一条,另外那条就会消失。有m扇门,每扇门上有两把锁,只要开启其中一把锁,那扇门就会被打开,每把锁只能由一种钥匙去开,给出每把锁能被哪条钥匙开启。还给出每种钥匙对应的两条钥匙编号。门只能从外往里开启,问怎样使打开的门最多,输出打开的门的最大值。
思路:对于每种钥匙,只能选择其中一条(对于一条钥匙i,i表示选择这条钥匙,i+2*n表示不选择这条钥匙),即对于x和y同属一种钥匙,加边x->y+2*n、y->x+2*n。对于每扇门,只要其中一把锁被开启就ok了,但也意味着肯定要开启其中一把,对于每扇门上的a、b两把锁,如果不开a锁,那么一定要开b锁,相应地,如果不开b锁,那么一定要开a锁,所以对于每扇门,加边a+2*n->b、b+2*n->a。另外用二分能加快速度。

AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <map>
#include <cstdlib>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define ll long long
using namespace std;

const int maxn=1<<12;
const int INF=1000000000;
struct node
{
    int v,next;
}edge[maxn*maxn];
int head[maxn],scc[maxn],stack[maxn],x[maxn],y[maxn];
int low[maxn],dfn[maxn],lock[maxn][2];
bool ins[maxn];
int n,m,num,cnt,top,snum;
void init()
{
    memset(head,-1,sizeof(head));
    num=0;
}
void add(int u,int v)
{
    edge[num].v=v;
    edge[num].next=head[u];
    head[u]=num++;
}
void input()
{
    for(int i=0;i<n;i++)
        scanf("%d%d",&x[i],&y[i]);
    for(int i=1;i<=m;i++)
    scanf("%d%d",&lock[i][0],&lock[i][1]);
}
void dfs(int u)
{
    int x;
    dfn[u]=low[u]=++cnt;
    stack[top++]=u;
    ins[u]=true;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(!dfn[v])
        {
            dfs(v);
            low[u]=min(low[u],low[v]);
        }
        else if(ins[v]) low[u]=min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u])
    {
        do{
            x=stack[--top];
            ins[x]=false;
            scc[x]=snum;
        }while(x!=u);
        snum++;
    }
}
void tarjan()
{
    memset(dfn,0,sizeof(dfn));
    memset(ins,false,sizeof(ins));
    cnt=top=snum=0;
    for(int i=0;i<4*n;i++)
    if(!dfn[i]) dfs(i);
}
bool judge(int f)
{
    init();
    for(int i=0;i<n;i++)
    {
        add(x[i],y[i]+2*n);
        add(y[i],x[i]+2*n);
    }
    for(int i=1;i<=f;i++)
    {
        add(lock[i][0]+2*n,lock[i][1]);
        add(lock[i][1]+2*n,lock[i][0]);
    }
    tarjan();
    for(int i=0;i<2*n;i++)
    if(scc[i]==scc[i+2*n])
    return false;
    return true;
}
void solve()
{
    int low=0,high=m;
    int mid,ans=0;
    while(low<=high)
    {
        mid=(low+high)>>1;
        if(judge(mid)) {
            ans=max(ans,mid);
            low=mid+1;
        }
        else high=mid-1;
    }
    printf("%d\n",ans);
}
int main()
{
    while(scanf("%d%d",&n,&m),n||m)
    {
       input();
       solve();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值