Get Luffy Out
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 Source
Beijing 2005
|
题目:http://poj.org/problem?id=2723
题意:给你n对钥匙,每对只能选一把来用,有m扇门,每扇门有两把钥匙都可以打开,问怎么选钥匙,使开的门最多,门得按顺序开
分析:一开始想不出钥匙间的对应关系,也就无法用2-sat建图,后来考虑了一会,发现不能选同一扇门对应两把钥匙的对钥匙(也就是同一组的另一把钥匙),现在给钥匙另一个编号,然后按照这个思路建图就行。。。由于m比较大,所有二分m判断可行性即可
代码:
#include<cstdio> #include<iostream> using namespace std; const int mm=4444; const int mn=2222; int ver[mm],next[mm]; int head[mn],dfn[mn],low[mn],q[mn],id[mn],x[mn],a[mn],b[mn]; int i,n,m,idx,cnt,top,edge,l,r,ans; void add(int u,int v) { ver[edge]=v,next[edge]=head[u],head[u]=edge++; } void dfs(int u) { dfn[u]=low[u]=++idx; q[top++]=u; for(int i=head[u],v;i>=0;i=next[i]) if(!dfn[v=ver[i]]) dfs(v),low[u]=min(low[u],low[v]); else if(!id[v])low[u]=min(low[u],dfn[v]); if(low[u]==dfn[u]) { id[u]=++cnt; while(q[--top]!=u)id[q[top]]=cnt; } } void Tarjan() { for(idx=cnt=top=i=0;i<n+n;++i)dfn[i]=id[i]=0; for(i=0;i<n+n;++i) if(!dfn[i])dfs(i); } bool ok() { for(edge=i=0;i<n+n;++i)head[i]=-1; for(i=0;i<m;++i) { add(x[a[i]],x[b[i]]^1); add(x[b[i]],x[a[i]]^1); } Tarjan(); for(i=0;i<n+n;i+=2) if(id[i]==id[i^1])return 0; return 1; } int main() { while(scanf("%d%d",&n,&m),n+m) { for(i=0;i<n;++i) { scanf("%d%d",&l,&r); x[l]=i<<1|1,x[r]=i<<1; } for(i=0;i<m;++i) scanf("%d%d",&a[i],&b[i]); ans=l=0,r=m; while(l<=r) { m=(l+r)>>1; if(ok())ans=m,l=m+1; else r=m-1; } printf("%d\n",ans); } return 0; }