poj2723 Get Luffy Out

网友投稿 547 2022-08-29

poj2723 Get Luffy Out

poj2723 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 <= 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#include#include#include#includeusing 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>1; if (check(mid)) l=mid+1;else r=mid-1; }printf("%d\n",r); } return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:来看看 MySQL 的这个 BUG,坑了多少人....(来看看表情包)
下一篇:bzoj1579&luogu2939 [USACO09FEB]改造路Revamping Trails
相关文章

 发表评论

暂时没有评论,来抢沙发吧~