博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1816 Get Luffy Out *
阅读量:6892 次
发布时间:2019-06-27

本文共 4305 字,大约阅读时间需要 14 分钟。

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 
#include
#include
#include
#include
#include
#include
using namespace std;const int N = (1<<13);int n , m , st[N<<1] ,top;int eh[N] , et[N*N] , nxt[N*N] , tot ;bool mark[N<<2];struct node{ int x , y ;} key[N<<1] , door[N<<1];void init(){ tot = 0 ; memset( eh , -1 , 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^1] ) return false ; mark[u] = true ; st[top++] = u ; for( int i = eh[u] ; ~i ; i = nxt[i] ){ int v = et[i]; if( !dfs(v^1) ) return false; } return true;}bool solve(){ for( int i = 0 ; i < 4 * n ; i += 2 ){ if( !mark[i] && !mark[i+1] ){ top = 0 ; if( !dfs(i) ){ while( top > 0 ) mark[ st[--top] ] = false ; if( !dfs(i+1) ) return false; } } } return true;}bool test( int dep ){ init(); for( int i = 0 ; i < n ; ++i ){ addedge( 2*key[i].x , 2*key[i].y ); } for( int i = 0 ; i < dep ; ++i ){ addedge(2*door[i].x^1,2*door[i].y^1); } return solve();}void run(){ int x , y ;// cout << N <
>1; if( test(mid) ) ans = mid , l = mid + 1 ; else r = mid - 1; } 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(); }}

 

 

转载于:https://www.cnblogs.com/hlmark/p/4021639.html

你可能感兴趣的文章
GO语言练习:网络编程 ICMP 示例
查看>>
ios11--UIButton
查看>>
阿里云海外征战记:跻身全球前三,只用了两年半
查看>>
解密回声消除技术之二(应用篇)
查看>>
Go语言的web程序写法
查看>>
IDF2011:基于SaaS模式的"教学云"案例
查看>>
《Linux From Scratch》第三部分:构建LFS系统 第七章:基本系统配置- 7.5. 配置系统时间...
查看>>
云计算你必须思考的8大问题
查看>>
Windows7 Debug Test
查看>>
HTTPS连接的前几毫秒发生了什么
查看>>
从变量到封装:一文带你为机器学习打下坚实的Python基础
查看>>
给大家共享一个基本算法包
查看>>
Riverbed:SDN向广域网扩展为企业带来哪些价值
查看>>
定义中国网络安全市场战略高度,绿盟科技为“互联网+”保驾护航
查看>>
python 自定义 包 模块 打包 安装
查看>>
ubuntu桌面的标题栏和启动栏消失问题[亲测可用]
查看>>
Cloud technology in today's job market
查看>>
3月31日云栖精选夜读:数据科学咨询:想要转型毫无头绪?看了本文你不慌
查看>>
程序猿日记S01E03
查看>>
如何解决域名解析不生效问题?
查看>>