poj 2732 Get Luffy Out(2分+2-sat)

Get Luffy Out
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 8671 Accepted: 3363

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

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-sat模型。二分+2-sat.

注意点:1.建图时要注意所用的模板,如果模板中要求成对的点是异或为1,需要离散一下。
        2.二分时候注意。如我的二分模板是右端取不到,那么初始右端就应该+1,否则取不到最后的那个值,在那个地方wa了好久........还是太菜

代码:
//#include<bits/stdc++h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<math.h>
#include<map>
#include<vector>
#include<set>
#include<queue>
using namespace std;
const int N = 2222;
const int M = N*N*16;
struct Edge
{
    int from, to, next;
} edge[M], edge2[M];
int head[N];
int cntE, cntE2;
void addedge(int u, int v)
{
    edge[cntE].from = u;
    edge[cntE].to = v;
    edge[cntE].next = head[u];
    head[u] = cntE++;
}
void addedge2(int u, int v)
{
    edge2[cntE2].from = u;
    edge2[cntE2].to = v;
    edge2[cntE2].next = head[u];
    head[u] = cntE2++;
}

int dfn[N], low[N], idx;
int stk[N], top;
int in[N];
int kind[N], cnt;

void tarjan(int u)
{
    dfn[u] = low[u] = ++idx;
    in[u] = true;
    stk[++top] = u;
    for (int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].to;
        if (!dfn[v]) tarjan(v), low[u] = min(low[u], low[v]);
        else if (in[v]) low[u] = min(low[u], dfn[v]);
    }
    if (low[u] == dfn[u])
    {
        ++cnt;
        while (1)
        {
            int v = stk[top--];
            kind[v] = cnt;
            in[v] = false;
            if (v == u) break;
        }
    }
}

int opp[N], ind[N], col[N]; // 相对的点 入度 染色 col[]=1选择

bool topsort(int n) // 序号从0开始
{
    for (int i = 0; i < 2*n; i += 2)
    {
        int k1 = kind[i];
        int k2 = kind[i^1]; // 相对的两个点
        if (k1 == k2) return false;
        opp[k1] = k2;
        opp[k2] = k1;
    }
    memset(head, -1, sizeof head);
    int u, v;
    for (int i = 0; i < cntE; ++i)
    {
        u = edge[i].from, v = edge[i].to;
        if (kind[u] != kind[v])   // 反向建图
        {
            addedge2(kind[v], kind[u]);
            ind[kind[u]]++;
        }
    }
    queue<int> q;
    for (int i = 1; i <= cnt; ++i) if (!ind[i]) q.push(i);
    while (q.size())
    {
        u = q.front();
        q.pop();
        if (!col[u]) col[u] = 1, col[ opp[u] ] = -1;
        for (int i = head[u]; i != -1; i = edge2[i].next)
            if (--ind[edge2[i].to] == 0) q.push(edge2[i].to);
    }
    return true;
}

void init()
{
    cntE = cntE2 = 0;
    memset(head, -1, sizeof head);
    memset(dfn, 0, sizeof dfn);
    memset(in, false, sizeof in);
    idx = top = cnt = 0;
    memset(ind, 0, sizeof ind);
    memset(col, 0, sizeof col);
}
int num[N],l[N],r[N];
map<int,int>p;
int main()
{
    int n;
    int m;
    while(scanf("%d%d",&n,&m)!=EOF&&n+m)
    {
        int a,b;
        p.clear();
        for(int i=0; i<n; i++)
        {
            scanf("%d%d",&a,&b);
            p[a]=i*2;
            p[b]=i*2+1;
        }
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&l[i],&r[i]);
            l[i]=p[l[i]];
            r[i]=p[r[i]];
        }
        int L=0,R=m+1;
        while(L+1<R)
        {
            init();
            int mid=(L+R)/2;
            for(int i=1; i<=mid; i++)
            {
                if((l[i]^1)==r[i])
                    continue;
                 if(l[i]==r[i])addedge(r[i],r[i]^1);
                else
                {
                    addedge(l[i],r[i]^1);
                    addedge(r[i],l[i]^1);
                }
            }
            for(int i=0; i<2*n; i++)
                if(!dfn[i])
                    tarjan(i);
            if(topsort(n))
                L=mid;
            else
                R=mid;
        }
        printf("%d\n",L);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值