poj2723Get Luffy Out 2-sat问题

Problem 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 http://poj.org/problem?id=2723 

【思路】

2sat问题可以先看伍昱 由对称性解2-sat问题(ppt)

2sat问题理解好几天才懂,好艰辛啊QwQ~~~

n对钥匙,每对最多取一个,这就是约束对啦~~~然后给定m个锁,每个锁对应两把钥匙,尽可能打开多的锁,然后这很明显就是2sat问题了。

首先我们需要二分答案,来打开尽可能多的门!!!

把一对钥匙看作一个变量,二元 分别表示选一对中的任意一把,所以我们扫描x扇门来添加边,如某扇门对应锁d1,d2,则添加边~d1->d2和~d2->d1;然后

tarjan一下求强联通,就ac啦!!!

 

 

AC代码:

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 int Max(int a, int b) { return a > b ? a : b; }
  4 int Min(int a, int b) { return a > b ? b : a; }
  5 #define FOR(i,a,b) for(int i=a;i<=b;i++)
  6 typedef long long LL;
  7 typedef unsigned long long ull;
  8 const double INF = 99999999999.0;
  9 const double eps = 1e-6;
 10 #define MAX_N 1<<10
 11 #define MAX_M 1<<11
 12 #define MAX_V 2*MAX_N
 13 int n,m,num,index,scc;
 14 int key[MAX_V],door1[MAX_M],door2[MAX_M],id[MAX_V],dfn[MAX_V],low[MAX_V];
 15 bool in[MAX_V];
 16 vector<int>g[MAX_V];
 17 stack<int>st;
 18 void init()
 19 {
 20     index=scc=0;
 21     memset(dfn,0,sizeof(dfn));
 22     memset(low,0,sizeof(low));
 23     memset(in,false,sizeof(in));
 24     memset(id,0,sizeof(id));
 25     FOR(i,1,n*2)g[i].clear();
 26     while(!st.empty())st.pop();
 27 }
 28 void add_edge(int u,int v)
 29 {
 30     g[u].push_back(v);
 31 }
 32 void tarjan(int u)
 33 {
 34     dfn[u]=low[u]=++index;
 35     st.push(u);
 36     in[u]=true;
 37     for(vector<int>::iterator it=g[u].begin();it!=g[u].end();it++){
 38         int tmp=*it;
 39         if(dfn[tmp]==0){
 40             tarjan(tmp);
 41             low[u]=min(low[u],low[tmp]);
 42         }
 43         else if(in[tmp]){
 44             low[u]=min(low[u],dfn[tmp]);
 45         }
 46     }
 47     if(dfn[u]==low[u]){
 48         ++scc;
 49         int v;
 50         do{
 51             v=st.top();st.pop();in[v]=false;
 52             id[v]=scc;
 53         }while(v!=u);
 54     }
 55 }
 56 bool sat(int u)
 57 {
 58     num=n*2;
 59     for(int i=0;i<num;i++){
 60         g[i].clear();
 61     }
 62     for(int i=1;i<=u;i++){
 63         add_edge(key[door1[i]],door2[i]);
 64         add_edge(key[door2[i]],door1[i]);
 65     }
 66     for(int i=0;i<num;i++)if(dfn[i]==0)
 67         tarjan(i);
 68     for(int i=0;i<num;i++){
 69         if(id[i]==id[key[i]])return false;
 70     }
 71     return true;
 72 }
 73 int solve()//二分答案解决
 74 {
 75     int low=1,high=m+1;//high 是达不到的
 76     while(high-low>1){
 77         int mid=(high+low)/2;
 78         init();
 79         if(sat(mid)){
 80             low=mid;
 81         }
 82         else high=mid;
 83     }
 84     return low;
 85 }
 86 int main()
 87 {
 88     while(scanf("%d %d",&n,&m)!=EOF&&n&&m){
 89         for(int i=1;i<=n;i++){
 90             int a,b;
 91             scanf("%d %d",&a,&b);
 92             key[a]=b;key[b]=a;
 93         }
 94         for(int i=1;i<=m;i++){
 95             scanf("%d %d",&door1[i],&door2[i]);
 96         }
 97         printf("%d\n",solve());
 98     }
 99     return 0;
100 }

 

第一次写,好鸡冻~~~

 

posted @ 2018-03-12 16:38  WindFreedom  阅读(361)  评论(0编辑  收藏  举报