poj 2723
Get Luffy Out
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 7295 | Accepted: 2778 |
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
Source
二分答案走2 -sat
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <stack> 6 #include <vector> 7 8 using namespace std; 9 10 const int MAX_N = (1 << 11) + 5; 11 int N,M; 12 int low[MAX_N],pre[MAX_N],cmp[MAX_N]; 13 int dfs_clock,scc_cnt; 14 stack <int > S; 15 int no[MAX_N],x[3000],y[3000]; 16 vector<int> G[MAX_N]; 17 18 19 void dfs(int u) { 20 pre[u] = low[u] = ++dfs_clock; 21 S.push(u); 22 for(int i = 0; i < G[u].size(); ++i) { 23 int v = G[u][i]; 24 if(!pre[v]) { 25 dfs(v); 26 low[u] = min(low[u],low[v]); 27 } else if(!cmp[v]) { 28 low[u] = min(low[u],pre[v]); 29 } 30 } 31 32 if(low[u] == pre[u]) { 33 ++scc_cnt; 34 for(;;) { 35 int x = S.top(); S.pop(); 36 cmp[x] = scc_cnt; 37 if(x == u) break; 38 } 39 } 40 } 41 void scc() { 42 dfs_clock = scc_cnt = 0; 43 memset(cmp,0,sizeof(cmp)); 44 memset(pre,0,sizeof(pre)); 45 46 for(int i = 0; i < 2 * N; ++i) { 47 if(!pre[i]) dfs(i); 48 } 49 } 50 bool check(int m) { 51 for(int i = 0; i < 2 * N; ++i) { 52 G[i].clear(); 53 } 54 for(int i = 1; i <= m; ++i) { 55 int a = x[i],b = y[i]; 56 G[no[a]].push_back(b); 57 G[no[b]].push_back(a); 58 } 59 scc(); 60 for(int i = 0; i < 2 * N; ++i) { 61 if(cmp[i] == 0) continue; 62 if(cmp[i] == cmp[ no[i] ]) { 63 //printf("i = %d no = %d %d %d\n",i,no[i],cmp[i],cmp[no[i]]); 64 return false; 65 } 66 } 67 68 return true; 69 } 70 void solve() { 71 int l = 0,r = M; 72 while(l < r) { 73 int mid = (l + r + 1) / 2; 74 if(check(mid)) l = mid; 75 else r = mid - 1; 76 } 77 printf("%d\n",l); 78 } 79 int main() 80 { 81 //freopen("sw.in","r",stdin); 82 while(~scanf("%d%d",&N,&M)) { 83 if(N == 0 && M == 0) break; 84 for(int i = 1; i <= N; ++i) { 85 int a,b; 86 scanf("%d%d",&a,&b); 87 no[a] = b; 88 no[b] = a; 89 } 90 for(int i = 1; i <= M; ++i) { 91 scanf("%d%d",&x[i],&y[i]); 92 } 93 94 solve(); 95 96 } 97 return 0; 98 }