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
- 题意:m个门,每个门上有两把锁,打开一个就可以通过,2n个钥匙,每两个绑在一起,只能选用一个 ,选了一个,另一个就被废弃。问最多可以通过几扇门?
- 2-sat问题关键在建图,2-sat对每个事物都有两个选项 ,选和不选. 可以这么建:
- 每把钥匙有两个状态(用或不用),把这作为2-sat的两个选项
- 然后是加条件,a、b绑在一起,则选a就不选b,选b就不选a,建边a->!b,b->!a
- c、d在同一个门上,则不开c就开d,不开d就开c,建边!c->d,!d->c
- 然后二分答案都可以了
1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<math.h> 7 #include<algorithm> 8 #include<queue> 9 #include<set> 10 #include<bitset> 11 #include<map> 12 #include<vector> 13 #include<stdlib.h> 14 #include <stack> 15 using namespace std; 16 #define PI acos(-1.0) 17 #define max(a,b) (a) > (b) ? (a) : (b) 18 #define min(a,b) (a) < (b) ? (a) : (b) 19 #define ll long long 20 #define eps 1e-10 21 #define MOD 1000000007 22 #define N 1<<16 23 #define M 1<<16 24 #define inf 1<<26 25 int n,m; 26 27 //////////////////////////////////////////////////////// 28 int tot; 29 int head[N]; 30 int vis[N]; 31 int tt; 32 int scc; 33 stack<int>s; 34 int dfn[N],low[N]; 35 int col[N]; 36 struct Node 37 { 38 int from; 39 int to; 40 int next; 41 }edge[N]; 42 void init() 43 { 44 tot=0; 45 scc=0; 46 tt=0; 47 memset(head,-1,sizeof(head)); 48 memset(dfn,-1,sizeof(dfn)); 49 memset(low,0,sizeof(low)); 50 memset(vis,0,sizeof(vis)); 51 memset(col,0,sizeof(col)); 52 while(!s.empty()){ 53 s.pop(); 54 } 55 } 56 void add(int s,int u)//邻接矩阵函数 57 { 58 edge[tot].from=s; 59 edge[tot].to=u; 60 edge[tot].next=head[s]; 61 head[s]=tot++; 62 } 63 void tarjan(int u)//tarjan算法找出图中的所有强连通分支 64 { 65 dfn[u] = low[u]= ++tt; 66 vis[u]=1; 67 s.push(u); 68 int cnt=0; 69 for(int i=head[u];i!=-1;i=edge[i].next) 70 { 71 int v=edge[i].to; 72 if(dfn[v]==-1) 73 { 74 // sum++; 75 tarjan(v); 76 low[u]=min(low[u],low[v]); 77 } 78 else if(vis[v]==1) 79 low[u]=min(low[u],dfn[v]); 80 } 81 if(dfn[u]==low[u]) 82 { 83 int x; 84 scc++; 85 do{ 86 x=s.top(); 87 s.pop(); 88 col[x]=scc; 89 vis[x]=0; 90 }while(x!=u); 91 } 92 } 93 bool two_sat(){ 94 95 for(int i=0;i<2*n*2;i++){ 96 if(dfn[i]==-1){ 97 tarjan(i); 98 } 99 } 100 for(int i=0;i<n*2;i++){ 101 if(col[2*i]==col[2*i+1]){ 102 return false; 103 } 104 } 105 return true; 106 } 107 //////////////////////////////////////// 108 int key1[N],key2[N]; 109 int door1[M],door2[M]; 110 111 bool solve(int mid){ 112 init(); 113 for(int i=1;i<=n;i++){ 114 add(2*key1[i],2*key2[i]+1); 115 add(2*key2[i],2*key1[i]+1); 116 } 117 118 for(int i=1;i<=mid;i++){ 119 add(2*door1[i]+1,2*door2[i]); 120 add(2*door2[i]+1,2*door1[i]); 121 } 122 if(two_sat()) return true; 123 return false; 124 } 125 126 int main() 127 { 128 while(scanf("%d%d",&n,&m)==2){ 129 if(n==0 && m==0){ 130 break; 131 } 132 133 134 135 for(int i=1;i<=n;i++){ 136 scanf("%d%d",&key1[i],&key2[i]); 137 } 138 for(int i=1;i<=m;i++){ 139 scanf("%d%d",&door1[i],&door2[i]); 140 } 141 142 143 int low=0; 144 int high=m+1; 145 // int ans; 146 while(low<high){ 147 int mid=(low+high)>>1; 148 if(solve(mid)){ 149 //ans=mid; 150 low=mid+1; 151 } 152 else{ 153 high=mid; 154 } 155 } 156 printf("%d ",low-1); 157 158 159 } 160 return 0; 161 }