`
Simone_chou
  • 浏览: 195907 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Get Luffy Out(2 - sat + 二分)

    博客分类:
  • POJ
 
阅读更多
Get Luffy Out
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7299   Accepted: 2781

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

 

       题意:

       每个 case 给出 N (1 ~ 2 ^ 10)和 M (1 ~ 2 ^ 11)。代表有 N 组钥匙配对关系和 M 道门,每道门都有两把可开钥匙,选其中一把开即可通过这道门。每对配对钥匙间只能用其中一条,输出最大能通过几道门。直到输出 0 0结束。

 

       思路:

       2 - sat。一个个从头到尾 2 - sat 的话无疑会TLE,故要二分答案,每二分一次就要重新建图一次。记得要 clear 可变数组。节点数是 2 * n 个,故 clear 的时候要 clear 2 * 2 * N = 4 * N 个。

 

       AC:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>

using namespace std;

const int NMAX = 5000;

int n, m;
int door[NMAX][4], d[NMAX][4];
vector<int> v[NMAX];

int dfs_clock, scc_cnt;
int pre[NMAX], low[NMAX], cmp[NMAX];
stack<int> s;

void dfs(int u) {
        pre[u] = low[u] = ++dfs_clock;
        s.push(u);

        for (int i = 0; i < v[u].size(); ++i) {
                if (!pre[ v[u][i] ]) {
                        dfs( v[u][i] );
                        low[u] = min(low[u], low[ v[u][i] ]);
                } else if (!cmp[ v[u][i] ]) {
                        low[u] = min(low[u], pre[ v[u][i] ]);
                }
        }

        if (pre[u] == low[u]) {
                ++scc_cnt;
                for (;;) {
                        int x = s.top(); s.pop();
                        cmp[x] = scc_cnt;
                        if (x == u) break;
                }
        }
}

void scc() {
        dfs_clock = scc_cnt = 0;
        memset(pre, 0, sizeof(pre));
        memset(cmp, 0, sizeof(cmp));

        for (int i = 0;i < 4 * n; ++i) {  //是4 * N
                if (!pre[i]) dfs(i);
        }
}

bool C(int k) {

        for (int i = 0; i < 4 * n; ++i)  //是4 * N
                v[i].clear();

        for (int i = 1; i <= n; ++i) {
                v[ d[i][1] ].push_back( d[i][2] + 2 * n);
                v[ d[i][2] ].push_back( d[i][1] + 2 * n);
        }

        for (int i = 1; i <= k; ++i) {
                v[ door[i][1] + 2 * n].push_back( door[i][2] );
                v[ door[i][2] + 2 * n].push_back( door[i][1] );
        }

        scc();

        for (int i = 0; i < 2 * n; ++i) {
                if (cmp[i] == cmp[i + 2 * n]) return false;
        }

        return true;
}

int main() {

        while (~scanf("%d%d", &n, &m) && (n + m)) {

                for (int i = 1; i <= n; ++i)
                        scanf("%d%d", &d[i][1], &d[i][2]);

                for (int i = 1; i <= m; ++i)
                        scanf("%d%d", &door[i][1], &door[i][2]);

                int l = 0, r = m + 1;
                while (r - l > 1) {
                        int mid = l + (r - l) / 2;
                        if (C(mid)) l = mid;
                        else r = mid;
                }

                printf("%d\n", l);
        }

        return 0;
}

 

 

分享到:
评论

相关推荐

    numpy-1.18.2+mkl-cp38-cp38-win32.whl

    python中的numpy库,版本为1.18.2,适用于win64操作系统,外网下载要好久,我下载好了,供大家参考,减少不必要的时间消耗。

    Luffy-s-blog:个人博客

    【Luffy-s-blog:个人博客】是一个开源项目,旨在记录开发者Luffy的个人学习心得与技术分享,为他的个人博客提供内容支持。这个项目的核心是使用HTML(HyperText Markup Language)来构建网页结构,展示了HTML在创建...

    Luffy_city-Vue:前端

    luffy_city 一个Vue.js项目构建设置# install dependenciesnpm install# serve with hot reload at localhost:8080npm run dev# build for production with minificationnpm run build# build for production and ...

    poj图论题目汇总

    #### 2723 Get Luffy Out - 2-SAT - **知识点**:2-SAT问题,一种特殊的布尔满足性问题。 #### 2724 Purifying Machine - 二分匹配 - **知识点**:二分匹配算法。 #### 2728 Desert King - 最优比例生成树 - **...

    前后端代码luffy-apiluffycity

    【标题】"前后端代码luffy-apiluffycity"所指的项目是一个结合了前端与后端开发的代码库,主要包含两个关键部分:luffy_api 和 luffycity。这个项目可能是一个完整的Web应用解决方案,其中luffy_api 代表后端服务,...

    二分查找解题

    二分查找,也被称为折半查找,是一种在有序数组中高效地查找特定元素的搜索算法。这个算法的主要思想是利用数组的有序性,通过不断缩小查找范围,直到找到目标值或者确定目标值不存在为止。本篇文章将深入探讨二分...

    c/c++二分查找修改版

    二分 if x s[middle] return middle; 找到并返回 else if x &lt; s[middle] 关键字小于中值 继续二分查找 并将上限改为middle BinarySearch s x low middle 1 ; else 关键字大于中值 继续二分查找 并将下限改为...

    轻量级的嵌入式FaaS引擎-luffy-main.zip

    noear::轻量级的嵌入式FaaS引擎(可按需组装),也可用作低代码引擎

    覆盖数字--英雄会pongo

    给定整数区间[a,b]和整数区间[x,y],你可以使用任意多次a,b之间的整数做加法,可以凑出多少个[x,y]区间... 问:2+3=5 1+4=5 这算1个还是2个? 答:算1次 问你能覆盖多少个不同的数字 [x,y]全覆盖住得话 就是y - x + 1。

    二分查找修改版

    //二分 if( x [middle]) //关键字小于中值,继续二分查找,并将上限改为middle BinarySearch(s, x, low, middle - 1); else if( x &gt; s[middle]) //关键字大于中值,继续二分查找,并将下限改为middle Binary...

    二分查找范例

    2. **时间复杂度**:二分查找的时间复杂度为O(log n),其中n是数组的大小。这是因为每次比较后,搜索范围都会减半,因此查找次数对数级增长。 3. **适用场景**:二分查找适用于静态数据结构,如数据库索引、电话簿...

    NavX 是一个小巧的导航工具;基于 Luffy faas 平台构建

    【NavX:小巧导航工具与Luffy FaaS平台的结合】 NavX,作为一个小巧而功能强大的导航工具,它为用户提供了一种高效的方式来进行系统或应用的导航。在日益复杂的IT环境中,工具的易用性和效率成为了关键因素,NavX...

    基于Python核心技术的Luffy项目设计源码与前端集成方案

    该项目是一个以Python为核心技术的Luffy项目,包括211个文件,涵盖67个pyc字节码文件、63个Python源代码文件、18个PNG图片文件、15个JavaScript文件、14个Vue模板文件、12个JPG图片文件、9个SVG矢量图文件、4个JPEG...

    正则表达式 正则软件

    二、正则表达式语法 1. 预定义字符类:`\d`(等同于[0-9]),`\D`(非数字),`\s`(空白字符,包括空格、制表符、换页符等),`\S`(非空白字符),`\w`(字母、数字、下划线),`\W`(非字母、数字、下划线)。 ...

    RobotFramework常用关键字(1).pdf

    2. **Log** - 用于打印日志信息,类似于 Python 的 `print` 函数,可以帮助跟踪测试执行过程中的状态和结果。 3. **Set Variable** - 用来定义和赋值变量,例如 `${a} = 'Hello World!!!'`,这在测试过程中用于存储...

    钢条切割-【算法导论-动态规划】

    比如r7,它有很多切割方案,1-6,2-5,3-4,2-2-3,1-1-5等等,这些过程如何自己来模拟的话实在是太费时间,但是我们想在切割7的时候前面都已经完成了,我们可以在前面的基础上进行切割,这时只要考虑1-6,2-5,3-4即可,...

    TeamX,功能不强但绝对小巧;基于 Luffy faas 平台构建

    基于 Luffy faas 平台构建。主要功能有:Wiki(团队词条,用于写接口文档也行...)。Planned(项目计划 和 个人日志)。比较兄弟产品,区别会很大;基于表格组件定制。Issues(问题管理,如缺陷、需求...)。比Bug...

    virtualbox-ext-vnc-6.1.22-2-x86_64.pkg.tar.zst

    virtualbox 6.1 extension VNC安装包(需要下载zstd工具进行解压)

    luffy

    路飞 Laravel 8 Heroku样板 系统要求 请确保您的开发环境中安装了以下软件。 PostgreSQL PHP 7.4 作曲家 NPM 入门 将.env.example复制到.env 运行composer install 运行npm install 运行php artisan migrate ...

    海贼桌面图标

    2. **图标格式**:常见的图标格式有ICO、PNG、ICNS等。ICO是Windows系统下的标准图标格式,可以包含多种分辨率和颜色深度的图像。PNG格式图标常用于Mac OS和一些现代软件,因其支持透明度而受到欢迎。 3. **自定义...

Global site tag (gtag.js) - Google Analytics