poj2723 Get Luffy Out

Posted zbtrs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj2723 Get Luffy Out相关的知识,希望对你有一定的参考价值。

Get Luffy Out
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9022   Accepted: 3503

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

分析:其实这道题的本质就是2-SAT问题,每一个门都是一个限制条件,如果我们要求最多通过的门数,可以按照顺序枚举,每次添加两对限制条件进去,看看有没有解就好了.如果想进一步优化可以用二分.
要注意的是可能会存在一个门需要的钥匙是相同的,这个时候我们就必须选了,也就是不选的点i‘连向选的点i.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <stack>

using namespace std;

const int maxn = 10010,maxm = 200010;

int n,m,key[maxn],a[maxn],b[maxn],pre[maxn],low[maxn],scc[maxn],dfs_clock,head[maxn],nextt[maxm],to[maxm],tot = 1,top;

stack<int> s;

void add(int x,int y)
{
    to[tot] = y;
    nextt[tot] = head[x];
    head[x] = tot++;
}

void tarjan(int u)
{
    pre[u] = low[u] = ++dfs_clock;
    s.push(u);
    for (int i = head[u];i;i = nextt[i])
    {
        int v = to[i];
        if (!pre[v])
        {
            tarjan(v);
            low[u] = min(low[u],low[v]);
        }
        else
        if (!scc[v])
        low[u] = min(low[u],pre[v]);
    }
    if (low[u] == pre[u])
    {
        top++;
        while (1)
        {
            int t = s.top();
            s.pop();
            scc[t] = top;
            if (t == u)
            break;
        } 
    }
}

bool solve()
{
    for (int i = 0; i < n * 2; i++)
    if (!pre[i])
    tarjan(i);
    for (int i = 0; i < n * 2; i += 2)
    if (scc[i] == scc[i ^ 1])
    return false;
    return true;
}

int main()
{
    while (scanf("%d%d",&n,&m) && (n || m))
    {
        memset(head,0,sizeof(head));
        tot = 1;
        for (int i = 0; i < n; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            key[u] = i * 2;
            key[v] = i * 2 + 1;
        }
        for (int i = 0; i < m; i++)
        scanf("%d%d",&a[i],&b[i]);
        for (int i = 0; i < m; i++)
        {
            if (a[i] == b[i])
            add(key[a[i]] ^ 1,key[a[i]]); //必须选,所以表示不选的点要连向选的点
            else
            {
                add(key[a[i]] ^ 1,key[b[i]]);
                add(key[b[i]] ^ 1,key[a[i]]);
            }
            memset(pre,0,sizeof(pre));
            memset(low,0,sizeof(low));
            memset(scc,0,sizeof(scc));
            top = 0;
            dfs_clock = 0;
            if (!solve())
            {
                printf("%d\n",i);
                break;
            }
            if (i == m - 1)
            printf("%d\n",m);
        }
    }


    return 0;
}

 

以上是关于poj2723 Get Luffy Out的主要内容,如果未能解决你的问题,请参考以下文章

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

POJ 2723 Get Luffy Out(2-SAT)

poj 2723 Get Luffy Out 2-SAT

POJ2723-Get Luffy Out(2-SAT)

poj2723 Get Luffy Out解题报告tarjan+2-SAT+二分

图论补完计划poj 2723(2-SAT)