Get Luffy Out *

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 570    Accepted Submission(s): 225

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 made N pairs,one key may be appear in some 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 <= 2^10) and M (1 <= M <= 2^11) 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 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

Hint

题目有更改!

 
Source
 
 
 
经典two-sat 加 2分 。 
构图的时候要有点技巧 。。 
我是用反向构边的 。
对于条件1 ,构一条 2*u - 2*v  的无向边表示 2*u ,  2*v  不能共存
对于二分出来的深度  构一条  2*u^1  -  2*v^1  的无向边表示   2*u , 2*v 至少有一个存在
 
然后邻接链表的空间开成N*N 比较安全 , N 要开得大一点 , 不然很容易报WA ~
 
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
using namespace std;
const int N = (<<); int n , m , st[N<<] ,top;
int eh[N] , et[N*N] , nxt[N*N] , tot ;
bool mark[N<<]; struct node
{
int x , y ;
} key[N<<] , door[N<<]; void init()
{
tot = ;
memset( eh , - , sizeof eh );
memset( mark ,false , sizeof mark );
} void addedge( int u , int v )
{
et[tot] = v , nxt[tot] = eh[u] , eh[u] = tot++ ;
et[tot] = u , nxt[tot] = eh[v] , eh[v] = tot++ ;
} bool dfs( int u )
{
if( mark[u] ) return true;
if( mark[u^] ) return false ;
mark[u] = true ;
st[top++] = u ;
for( int i = eh[u] ; ~i ; i = nxt[i] ){
int v = et[i];
if( !dfs(v^) ) return false;
}
return true;
} bool solve()
{
for( int i = ; i < * n ; i += ){
if( !mark[i] && !mark[i+] ){
top = ;
if( !dfs(i) ){
while( top > ) mark[ st[--top] ] = false ;
if( !dfs(i+) ) return false;
}
}
}
return true;
} bool test( int dep )
{
init();
for( int i = ; i < n ; ++i ){
addedge( *key[i].x , *key[i].y );
}
for( int i = ; i < dep ; ++i ){
addedge(*door[i].x^,*door[i].y^);
}
return solve();
} void run()
{
int x , y ;
// cout << N <<endl;
for( int i = ; i < n ; ++i ){
scanf("%d%d",&key[i].x,&key[i].y);
} for( int i = ; i < m ; ++i ){
scanf("%d%d",&door[i].x,&door[i].y);
} int l = , r = m , ans = ;
while( l <= r )
{
int mid = ( l+r )>>;
if( test(mid) ) ans = mid , l = mid + ;
else r = mid - ;
}
printf("%d\n",ans);
} int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
while( scanf("%d%d",&n,&m) ){
if( !n && !m ) break;
run();
}
}

最新文章

  1. java的栈图形演示
  2. Node.js用ES6原生Promise对异步函数进行封装
  3. Java多线程开发系列之番外篇:事件派发线程---EventDispatchThread
  4. 用jQuery编的一个分页小代码
  5. UVa 11300 Spreading the Wealth(有钱同使)
  6. 微信域名weixin.com天价成交!是腾讯吗?
  7. webstorm &amp; phpstorm破解
  8. webpack echarts配置实例
  9. [Angular Tutorial]PhoneCat Tutorial App
  10. Spark机器学习之协同过滤算法
  11. 如何用jQuery实现div随鼠标移动而移动(详解)?----2017-05-12
  12. Numpy入门 - 行列式转置
  13. Linux服务器配置(一)
  14. 蓝桥每周一题之1. 3n+1 问题
  15. Maven遇到github引用的项目有bug怎么办?
  16. 算法提高 11-1实现strcmp函数
  17. MyEclipse如何清除废弃的工作空间
  18. 类的调用1(被调用的MyFirstJava)
  19. 局域网内远程连接OPC配置方法详解
  20. BBS--后台管理页面,编辑文章,xss攻击

热门文章

  1. 从0构建webpack开发环境(三) 开发环境以及 webpack-dev-server 的使用
  2. day04 列表增删改查、元祖以及range
  3. python浮点数与整数间的转化
  4. 解决git status中文路径乱码
  5. Sass-加法
  6. 16_3.jdk动态代理与aop
  7. pugixml的使用
  8. C#高级编程笔记 (6至10章节)运算符/委托/字符/正则/集合
  9. 测试md代码折叠功能
  10. ConcurrentHashMap1.7源码分析