现在的位置: 首页 > 综合 > 正文

POJ2723 Get Luffy Out 2-sat+二分

2014年10月11日 ⁄ 综合 ⁄ 共 3557字 ⁄ 字号 评论关闭

二分写搓了,tle了两次。。。。。。。。。

标准的2SAT

二分能开的最多的门数,然后重新建图,2SAT判断是否成立,之后输出答案即可。建图的时候注意,每层楼的两扇门至少要开一个。

一共有两种条件。一种是一组的钥匙只能用其中一把,另一个条件是每层楼的门至少要开一扇。

Get Luffy Out
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 6475   Accepted: 2453

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

 

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>

using namespace std;

#define MAXN 10000

struct node
{
	int to,next;
};

node edge[9000010],edge2[9000010];
int head[MAXN],en,dag[MAXN],en2,n,m;


void add(int a,int b)
{
	edge[en].to=b;
	edge[en].next=head[a];
	head[a]=en++;
}


int low[MAXN],dfn[MAXN];
int stack[MAXN],top,set[MAXN],col,num;
bool vis[MAXN],instack[MAXN];

void tarjan(int u)
{
	vis[u]=1;
	dfn[u]=low[u]=++num;
	instack[u]=true;
	stack[++top]=u;
	for(int i=head[u];i!=-1;i=edge[i].next)
	{
		int v=edge[i].to;
		if(!vis[v])
		{
			tarjan(v);
			low[u]=min(low[u],low[v]);
		}
		else
			if(instack[v])
				low[u]=min(dfn[v],low[u]);
	}
	if (dfn[u]==low[u])
	{
		int j;
		col++;
		do
		{
			j=stack[top--];
			instack[j]=false;
			set[j]=col;
		}
		while (j!=u);
	}
}

void solve()
{
	int i;
	top=col=num=0;
	memset(instack,0,sizeof(instack));
	memset(vis,0,sizeof(vis));
	for (i=0;i<2*n;i++)
		if (!vis[i])
			tarjan(i);
}

bool twosat()
{
    solve();
    for(int i=0;i<n*2;i++)
        if(set[i]==set[i+2*n]) return false;
   return true;

}

int k1[MAXN],k2[MAXN];
int a[MAXN],b[MAXN];

void  build(int x)
{
     memset(head,-1,sizeof(head)),en=0;
    for(int i=0;i<n;i++)
        add(k1[i],k2[i]+2*n),add(k2[i],k1[i]+2*n);
    for(int i=0;i<x;i++)
    {
        add(a[i]+2*n,b[i]);
        add(b[i]+2*n,a[i]);
    }

}

int main()
{
    while(~scanf("%d%d",&n,&m) && (n||m))
    {
        for(int i=0;i<n;i++)
            scanf("%d%d",&k1[i],&k2[i]);
        for(int i=0;i<m;i++)
            scanf("%d%d",&a[i],&b[i]);
        int l,r,mid,ans=0;
        l=0,r=m;
        while(l<=r)
        {
            mid=(l+r)>>1;
            build(mid);
            if(twosat())
                l=mid+1,ans=mid;
            else
                r=mid-1;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

抱歉!评论已关闭.