Get Luffy Out
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8211   Accepted: 3162

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

题意:有m层楼,从一层到m层,要进入每层都要打开位于该层的两道门中的至少一道。门锁有2n种,每个门锁为2n种中的一种,可以重复。有2n把钥匙,分别对应2n种锁,但是钥匙两两一组,共n组,每组只能选一个来开门,被选中的可以多次使用,另一个一次都不能用。问最多能上多少层。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf =0x7f7f7f7f;
const double pi=acos(-1);
const int maxn=100+1000; int n,m,pre[2*maxn],lowlink[2*maxn],dfs_clock,sccno[2*maxn],scc_cnt;
int door[2*maxn][3],key[2*maxn]; vector<int> G[2*maxn];
stack<int> S; void readkey()
{
int u,v;
scanf("%d %d",&u,&v);
key[u]=v;
key[v]=u;
} void readdoor(int i)
{
int u,v;
scanf("%d %d",&u,&v);
door[i][0]=u;
door[i][1]=v;
} void tarjan(int u)
{
pre[u]=lowlink[u]=++dfs_clock;
S.push(u);
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(pre[v]==-1)
{
tarjan(v);
lowlink[u]=min(lowlink[u],lowlink[v]);
}
else if(!sccno[v])
lowlink[u]=min(lowlink[u],pre[v]);
} if(lowlink[u]==pre[u])
{
scc_cnt++;
while(1)
{
int x=S.top();S.pop();
sccno[x]=scc_cnt;
if(x==u) break;//找到了当前强连通的起始节点就退出,<br>//不然会破坏其他强连通分量
}
}
} void find_scc()
{
MM(pre,-1);
MM(sccno,0);
MM(lowlink,0);
scc_cnt=dfs_clock=0;
for(int i=0;i<2*n;i++)
if(pre[i]==-1)
tarjan(i);
} bool ok(int mid)
{
for(int i=0;i<2*n;i++)
{G[i].clear();} for(int i=1;i<=mid;i++)
{
int a=door[i][0],b=door[i][1];
G[key[a]].push_back(b);
G[key[b]].push_back(a);
}//建图,预先记录门与钥匙 find_scc();
for(int i=0;i<2*n;i++)
if(sccno[i]==sccno[key[i]])
return false;
return true;
} int main()
{
while(~scanf("%d %d",&n,&m)&&(n||m))
{
MM(key,0);MM(door,0);
for(int i=1;i<=n;i++)
readkey();
for(int i=1;i<=m;i++)
readdoor(i); int l=0,r=m+1;
while(r-l>1)
{
int mid=(l+r)>>1;
if(ok(mid)) l=mid;
else r=mid;
}
printf("%d\n",l);
}
return 0;
}

  分析:经典题型,建图不再是以前的那种一个点对应两个(0,1)状态,而是一组数对应两个状态(只能取其中两个中的一个)

建图核心思路:对于确切的层数,对于一组数,设其中一个为x,则对应的另一个为f[x],如果一个门对应的数值为x,y两个,那么可以确定的是如果选f[x]的话,为了打开这门,必须选y,同理选f[y]的话,必须选x;

建图即可;再二分查找即可,tarjan求强连通复杂度(n+m)

易错点:

1,数组要开两倍,因为输入的n是组数;

TTTTTTTTTTTTTTTT POJ 2723 楼层里救朋友 2-SAT+二分的更多相关文章

  1. POJ 2723 Get Luffy Out(2-SAT+二分答案)

    Get Luffy Out Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8851   Accepted: 3441 Des ...

  2. HDU 1816, POJ 2723 Get Luffy Out(2-sat)

    HDU 1816, POJ 2723 Get Luffy Out pid=1816" target="_blank" style="">题目链接 ...

  3. poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分

    poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...

  4. poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分, dinic, isap

    poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分 dinic /* * Author: yew1eb * Created Time: 2014年10月31日 星期五 ...

  5. poj 2723 Get Luffy Out 二分+2-sat

    题目链接 给n个钥匙对, 每个钥匙对里有两个钥匙, 并且只能选择一个. 有m扇门, 每个门上有两个锁, 只要打开其中一个就可以通往下一扇门. 问你最多可以打开多少个门. 对于每个钥匙对, 如果选择了其 ...

  6. poj 2723

    Get Luffy Out Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7295   Accepted: 2778 Des ...

  7. poj 2723 2-SAT问题

    思路:二分枚举能开的门的数量,将每次枚举转换成2-SAT问题.这里存在的矛盾是假设有门上a,b两个锁,a锁对应于1号钥匙,而一号钥匙的配对是2号钥匙,b锁对应于3号钥匙,3号的配对是4号钥匙.那么2号 ...

  8. poj 2723 Get Luffy Out(2-sat)

    Description Ratish is a young man who always dreams of being a hero. One day his friend Luffy was ca ...

  9. POJ 2723 HDU 1816 Get Luffy Out

    二分答案 + 2-SAT验证 #include<cstdio> #include<cstring> #include<cmath> #include<stac ...

随机推荐

  1. Linux学习笔记(16)Linux前后台进程切换(fg/bg/jobs/ctrl+z)

    关键词:Linux前后台进程切换,linux进程切换 fg.bg.jobs.&.ctrl + z都是跟系统任务有关的,虽然现在基本上不怎么需要用到这些命令,但学会了也是很实用的一.& ...

  2. Java基础(八)

    IO流 概述与分类 Java中的IO流技术主要用于传输数据.典型的应用场景有:读写本地文件,上传下载文件等.按照数据传输的方向可以分为两种: l  输入流(Input):既让数据进入应用程序中. l  ...

  3. ubuntu 18.04 配置notebook远程连接的坑

    jupyter-notebook的安装不在此说明 在网上搜了很多方案都不行,好不容易从坑里爬出来 以下为远程连接配置方法 1.生成配置文件 jupyter notebook --generate-co ...

  4. Caffe常用算子GPU和CPU对比

    通过整理LeNet.AlexNet.VGG16.googLeNet.ResNet.MLP统计出的常用算子(不包括ReLU),表格是对比. Prelu Cpu版 Gpu版 for (int i = 0; ...

  5. Linux查看修改文件句柄数

    SuSE 11SP3 默认句柄数是1024 1.查看linux的文件句柄数 ulimit -a 2.修改文件句柄数 ①ulimit -n 65535②修改linux系统参数.vi /etc/secur ...

  6. python django 重新安装不能创建项目

    这里仅给大家做个思路提醒: 1.如果在别的地方找到一样的问题那就按别的方法去解决 2.如果是创建startproject的时候 报错:no module named 'mysite'  这个的话就和 ...

  7. Python 2中万恶的字符编码

    Python2中如果文件存在中文,必须要指定#-*- coding:utf8 -*-或#coding:utf8,否则会报错.那这是为什么呢? 一.原理解析 我们知道,在计算机发展初期,计算机只能识别字 ...

  8. 【LeetCode】451-根据字符出现频率排序

    题目描述 给定一个字符串,请将字符串里的字符按照出现的频率降序排列. 示例 1: 输入: "tree" 输出: "eert" 解释: 'e'出现两次,'r'和' ...

  9. hive不分区增量更新

    insert overwrite table ods.zeg_so select *,case when zsm.id is not null then cast(current_timestamp ...

  10. 设置apache服务器的访问证书,支持https访问,windows

    windows下载安装openssl http://slproweb.com/products/Win32OpenSSL.html windows证书的生成 安装成功后命令行执行 1.私钥,生成的文件 ...