http://www.elijahqi.win/archives/3079
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
因为门要求一个一个来打开 所以具有单调性 是可以二分的 每次二分我最远一个到达的门是多少然后2-sat建图 tarjan缩点验证是否可行
如果是钥匙则限制条件为选这个不可选那个
如果是门则限制为不选这个就得选那个
#include<queue>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
using namespace std;
inline char gc(){
static char now[1<<16],*S,*T;
if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if(T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while(!isdigit(ch)) {if (ch=='-') f=-1;ch=gc();}
while(isdigit(ch)) x=x*10+ch-'0',ch=gc();
return x*f;
}
const int N=2e4+10;
const int M=1e5;
struct node{
int x,y;
}key[N],dor[N];
struct node1{
int y,next;
}data[M<<1];bool stackf[N];
int num,h[N],dfn[N],low[N],q[N],top,s,b[N],n,m;
inline void insert1(int x,int y){
data[++num].y=y;data[num].next=h[x];h[x]=num;
}
inline void tarjan(int x){
dfn[x]=low[x]=++num;stackf[x]=1;q[++top]=x;
for (int i=h[x];i;i=data[i].next){
int y=data[i].y;
if (!dfn[y]) tarjan(y),low[x]=min(low[x],low[y]);
else if (stackf[y]) low[x]=min(low[x],dfn[y]);
}
if (dfn[x]==low[x]){
int y;++s;
do{y=q[top--];stackf[y]=0;b[y]=s;
}while(y!=x);
}
}
inline bool check(int md){
num=0;memset(h,0,sizeof(h));s=0;static int x,y;
memset(dfn,0,sizeof(dfn));
for (int i=1;i<=n;++i){
x=key[i].x;y=key[i].y;
insert1(x<<1,y<<1|1);insert1(y<<1,x<<1|1);
}
for (int i=1;i<=md;++i){
x=dor[i].x,y=dor[i].y;
insert1(x<<1|1,y<<1);insert1(y<<1|1,x<<1);
}num=0;
for (int i=0;i<n<<1;++i) if (!dfn[i]) tarjan(i);
for (int i=0;i<n;++i) if(b[i<<1]!=0&&b[i<<1]==b[i<<1|1]) return 0;
return 1;
}
int main(){
//freopen("poj2723.in","r",stdin);
while(1){
n=read();m=read();if (!n&&!m) return 0;
for (int i=1;i<=n;++i) key[i].x=read(),key[i].y=read();
for (int i=1;i<=m;++i) dor[i].x=read(),dor[i].y=read();
static int l,r,mid;l=1;r=m;
while(l<=r){
mid=l+r>>1;
if (check(mid)) l=mid+1;else r=mid-1;
}printf("%d\n",r);
}
return 0;
}