首页 > 代码库 > POJ2723-Get Luffy Out(2-SAT)
POJ2723-Get Luffy Out(2-SAT)
Get Luffy Out
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 7456 | Accepted: 2835 |
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 <= 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
题意:n对钥匙,m扇门,第i对钥匙表示两者不能同时选,第i扇门需要所需两个钥匙中的一个,只有开第i扇门,才能开第i+1扇门,问最多能开多少扇门。
思路:2-SAT。二分答案,根据第i对钥匙 a,b: a->b+2n, b->a+2n 对于第i扇门 所需的两把钥匙a,b: a+2n->b,b+2n->a
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <string> #include <algorithm> #include <queue> #include <stack> using namespace std; const int maxn = 1100*4; struct ee{ int u,v; }ee[maxn*10]; struct edge{ int v,nxt; }e[maxn*maxn]; int dfs_clk,scc_cnt; int sccno[maxn]; int lown[maxn],dfn[maxn]; int head[maxn]; stack<int> S; int nume; int n,m; void init(){ nume = 1; memset(dfn,0,sizeof dfn); memset(sccno,0,sizeof sccno); scc_cnt = dfs_clk = 0; memset(head,0,sizeof head); while(!S.empty()) S.pop(); } void addedge(int u,int v){ e[++nume].nxt = head[u]; e[nume].v = v; head[u] = nume; } void Tarjan(int u){ S.push(u); lown[u] = dfn[u] = ++dfs_clk; for(int i = head[u]; i; i = e[i].nxt){ int v = e[i].v; if(!dfn[v]){ Tarjan(v); lown[u] = min(lown[u],lown[v]); } else if(!sccno[v]){ lown[u] = min(lown[u],dfn[v]); } } if(lown[u]==dfn[u]){ scc_cnt++; while(true){ int x = S.top(); S.pop(); sccno[x] = scc_cnt; if(x == u) break; } } } bool can(int x){ init(); for(int i = 0; i < n; i++){ addedge(ee[i].v,ee[i].u+2*n); addedge(ee[i].u,ee[i].v+2*n); } for(int i = 0; i < x; i++){ addedge(ee[i+n].u+2*n,ee[i+n].v); addedge(ee[i+n].v+2*n,ee[i+n].u); } for(int i = 0; i < 4*n; i++){ if(!dfn[i]) Tarjan(i); } for(int i = 0; i < 2*n; i++){ if(sccno[i]==sccno[i+2*n]) return false; } return true; } int binary_search(int L,int R){ while(L <= R){ int mid = (L+R)>>1; if(can(mid)){ L = mid+1; }else{ R = mid-1; } } return R; } int main(){ while(~scanf("%d%d",&n,&m) && n+m){ for(int i = 0; i < n; i++) scanf("%d%d",&ee[i].u,&ee[i].v); for(int i = 0; i < m; i++) scanf("%d%d",&ee[i+n].u,&ee[i+n].v); int d = binary_search(0,m); printf("%d\n",d); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。