Get Luffy Out
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6609 | Accepted: 2531 |
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?
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
Source
【题目大意】
有2n把钥匙,分成2组,给你每组的钥匙信息,并且每组的钥匙只能用一个。
有m个门,每个门有2个锁,只要打开一个锁这个门就开了。(顺序遇见m个门)
问你最多能够打开多少个门。
【解题思路】
因为只能门只能按照顺序打开,所以很自然二分是个很好的选择。
建图的依据:
1、每对钥匙a,b有(a->!b),(b->!a) 也就是 a and b = 0 a被用b不被用,或b被用a不被用,或a,b都不被用
2、每善门对应锁的钥匙a,b有(!a->b),(!b->a) 也就是 a or b = 1,用a能打开用b不能打开,或用b能打开用a不能打开,或用a能打开用b也能打开
这道2-sat题的很不错,要做两个准备,其一,要二分可以到达的层数,这样就可以运用2-sat来解决了,但还是有个困难,那就是怎样确定矛盾,如果某一层的锁是2和3,而1和2以及3和4是相互配对的,那么1和4就是矛盾的,2和3也是矛盾的,可以假设1已经被使用,那么2就不能被使用,打开这一层只能靠3,也就不能选择4,所以1和4就是矛盾的,同理,2和3也是矛盾的。接下来就很简单了
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int VM=4200;
const int EM=10010;
struct Edge{
int to,nxt;
}edge[EM<<1];
int n,m,cnt,dep,top,atype,head[VM]; //atype 强连通分量的个数
int dfn[VM],low[VM],vis[VM],belong[VM],x[VM],y[VM];
int stack[VM],hash[VM];
void Init(){
cnt=0, atype=0, dep=0, top=0;
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(belong,0,sizeof(belong));
}
void addedge(int cu,int cv){
edge[cnt].to=cv; edge[cnt].nxt=head[cu]; head[cu]=cnt++;
}
void Tarjan(int u){
dfn[u]=low[u]=++dep;
stack[top++]=u;
vis[u]=1;
for(int i=head[u];i!=-1;i=edge[i].nxt){
int v=edge[i].to;
if(!dfn[v]){
Tarjan(v);
low[u]=min(low[u],low[v]);
}else if(vis[v])
low[u]=min(low[u],dfn[v]);
}
int j;
if(dfn[u]==low[u]){
atype++;
do{
j=stack[--top];
belong[j]=atype;
vis[j]=0;
}while(u!=j);
}
}
int solve(int mid){
Init();
for(int i=0;i<mid;i++){
addedge(x[i],hash[y[i]]);
addedge(y[i],hash[x[i]]);
}
for(int i=0;i<2*n;i++)
if(!dfn[i])
Tarjan(i);
for(int i=0;i<n;i++)
if(belong[i]==belong[hash[i]])
return 0;
return 1;
}
int main(){
//freopen("input.txt","r",stdin);
while(~scanf("%d%d",&n,&m)){
if(n==0 && m==0)
break;
int u,v;
for(int i=0;i<n;i++){
scanf("%d%d",&u,&v);
hash[u]=v; hash[v]=u; //标记配对的两个元素
}
for(int i=0;i<m;i++)
scanf("%d%d",&x[i],&y[i]);
int l=0,r=m,mid,ans=0;
while(l<=r){
mid=(l+r)>>1; //这里的二分就是确定可以到达的层数
if(solve(mid)){ //而对于某个确定的层,则就可以用2-sat来解决了
l=mid+1;
ans=mid;
}else
r=mid-1;
}
/*下面这个二分只须16ms。。。。
int l=0,r=m+1,mid;
while(l<r-1){
mid=(l+r)>>1;
if(solve(mid))
l=mid;
else
r=mid;
}
*/
printf("%d\n",ans);
}
return 0;
}