Poj 2723 Get Luffy Out【2-SAT---------Tarjan强连通】

Get Luffy Out

Time Limit: 2000MS

 

Memory Limit: 65536K

Total Submissions: 8270

 

Accepted: 3190

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、有n对钥匙,对应就是a和!a,选了其中一个,就不能选另一个,符合2-sat的选取条件,对应m道门,m道门都有两个钥匙孔,那么对应钥匙孔上的两把钥匙就是矛盾边,也就是矛盾关系。如果(a,b)有矛盾关系,根据2-sat解题步骤,我们应该建:(a,!b)和(b,!a)两条边,对于每一层,加入两条边,知道加入的边使得(a,!a)被染成一个颜色的时候,退出,或者是打开了所有门之后,退出。


2、Tarjan求一下强连通,就能ac啦!


AC代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int head[100000];
struct EdgeNode
{
    int to;
    int w;
    int next;
}e[100000];
int xx[100000];
int yy[100000];
int vis[100000];
int dfn[100000];
int low[100000];
int color[100000];
int stack[1000000];
int n,m,cont,sig,tt,cnt;
int a[1050][2];
void add(int from,int to)
{
    e[cont].to=to;
    e[cont].next=head[from];
    head[from]=cont++;
}
void Tarjan(int u)
{
    vis[u]=1;
    low[u]=dfn[u]=cnt++;
    stack[++tt]=u;
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        if(vis[v]==0)Tarjan(v);
        if(vis[v]==1)low[u]=min(low[u],low[v]);
    }
    if(dfn[u]==low[u])
    {
        sig++;
        do
        {
            color[stack[tt]]=sig;
            vis[stack[tt]]=-1;
        }
        while(stack[tt--]!=u);
    }
}
int Slove()
{
    memset(vis,0,sizeof(vis));
    sig=0;tt=-1;cnt=1;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<2;j++)
        {
            if(vis[a[i][j]]==0)
            {
                Tarjan(a[i][j]);
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        if(color[a[i][0]]==color[a[i][1]])return 0;
    }
    return 1;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0)break;
        cont=0;
        memset(head,-1,sizeof(head));
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&a[i][0],&a[i][1]);
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&xx[i],&yy[i]);
        }
        int k;
        for(k=1;k<=m;k++)
        {
            int x,y;
            x=xx[k];
            y=yy[k];
            int u,u2,v,v2;
            for(int i=0;i<n;i++)
            {
                if(a[i][0]==x||a[i][1]==x)
                {
                    u=i;
                    if(a[i][0]==x)u2=0;
                    else u2=1;
                }
                if(a[i][0]==y||a[i][1]==y)
                {
                    v=i;
                    if(a[i][0]==y)v2=0;
                    else v2=1;
                }
            }
           // printf("%d %d\n",a[u][u2],a[v][1-v2]);
            //printf("%d %d\n",a[v][v2],a[u][1-u2]);
            add(a[u][u2],a[v][1-v2]);
            add(a[v][v2],a[u][1-u2]);
            if(Slove()==0)
            {
                break;
            }
        }
        printf("%d\n",k-1);
    }
}








  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值