poj 2723 Get Luffy Out 【2-sat + 二分查找判断可行性】

来源:互联网 发布:广东省网络问政平台 编辑:程序博客网 时间:2024/05/31 19:45
Get Luffy Out
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 7914 Accepted: 3030

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 60 31 24 50 10 24 14 23 52 20 0

Sample Output

4


题意:你有N对钥匙,对应着2*N把不同种类的锁。对于每一对钥匙a和b,若你使用了a,那么b消失不会出现,若你使用了b则a消失。给你M个门,每个门上有两个锁(可能一样,也可能不一样),你只要打开其中一把锁就可以打开这个门。现在要求必须按输入数据的顺序来依次打开门,问你用这N对钥匙最多能打开多少门。


思路:对于任一个钥匙i,设Ai为使用i,!Ai为不使用i

一:对于每对钥匙a和b,有Aa -> !Ab 和 Ab -> !Aa

二:对于每扇门上的钥匙a和b,

1,若a,b不同,则有!Aa -> Ab 和 !Ab -> Aa;

2,若a,b相同,则有!Aa -> Aa即必选a。


实现:二分查找区间1-M,每次重建图tarjan求SCC并判断可行性。


AC代码:注意二分查找时的区间更替


#include <cstdio>#include <cstring>#include <queue>#include <stack>#include <vector>#include <map>#include <algorithm>#define MAXN 40000#define MAXM 200000#define INF 1000000#define eps 1e-5using namespace std;struct Edge{int from, to, next;}edge[MAXM];struct rec{int one, two;}door[MAXN];//记录每个门的钥匙 int head[MAXN], edgenum;int low[MAXN], dfn[MAXN];int dfs_clock;int sccno[MAXN], scc_cnt;stack<int> S;bool Instack[MAXN];int key[MAXN], keyfp[MAXN];//存储成对的钥匙 //map<int, int> fp;//对立钥匙 //map<int, int> first;//第一个钥匙 //map<int, int> second;//第二个钥匙int N, M;void init(){edgenum = 0;memset(head, -1, sizeof(head));}void addEdge(int u, int v){Edge E = {u, v, head[u]};edge[edgenum] = E;head[u] = edgenum++;}void input(){for(int i = 0; i < N; i++) scanf("%d%d", &key[i], &keyfp[i]);//addEdge(a, b + 2*N);//用过a不能用b//addEdge(b, a + 2*N);//用过b不能用a for(int i = 1; i <= M; i++) scanf("%d%d", &door[i].one, &door[i].two);}void getMap(int num)//打开前num个门 {int a, b;for(int i = 0; i < N; i++) {a = key[i]; b = keyfp[i];addEdge(a, b + 2*N);//用过a不能用baddEdge(b, a + 2*N);//用过b不能用a }  for(int i = 1; i <= num; i++){int u = door[i].one;int v = door[i].two;if(u == v)//同一个钥匙 addEdge(u + 2*N, u);else{addEdge(u + 2*N, v);//不用u 开 要用v开 addEdge(v + 2*N, u);//不用v 开 要用u开 } }}void tarjan(int u, int fa){int v;low[u] = dfn[u] = ++dfs_clock;S.push(u);Instack[u] = true;for(int i = head[u]; i != -1; i = edge[i].next){v = edge[i].to;if(!dfn[v]){tarjan(v, u);low[u] = min(low[u], low[v]);}else if(Instack[v])low[u] = min(low[u], dfn[v]);}if(low[u] == dfn[u]){scc_cnt++;for(;;){v = S.top(); S.pop();Instack[v] = false;sccno[v] = scc_cnt;if(v == u) break; } }} void find_cut(int l, int r){memset(low, 0, sizeof(low));memset(dfn, 0, sizeof(dfn));memset(sccno, 0, sizeof(sccno));memset(Instack, false, sizeof(Instack));dfs_clock = scc_cnt = 0;for(int i = l; i <= r; i++)if(!dfn[i]) tarjan(i, -1);} bool two_sat(int num)//判断打开前num个门 是否成立 {for(int i = 0; i < 2*N; i++){if(sccno[i] == sccno[i + 2*N])return false; }return true;}void solve()//二分搜索 {int left, right, mid, ans;left = 0, right = M;while(right >= left){ mid = (right + left) / 2;//printf("%d\n", mid);init();//初始化 getMap(mid);//打开前mid个门 建图 find_cut(0, 4*N-1); if(two_sat(mid)) {ans = mid;left = mid + 1;//注意这里 要不会死循环 }else {//ans = mid;right = mid - 1;}}printf("%d\n", ans);}int main(){while(scanf("%d%d", &N, &M), N||M){input();solve();} return 0;} 


0 0
原创粉丝点击