PKU-2723 Get Luffy Out(2-SAT+二分)
Get Luffy Out#
题目链接
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
题意:#
其实就是要有n对钥匙,钥匙用了一把就不能用另一把,还有m个门,每个门能用某两把钥匙开门,
问最多开多少个门
解题思路:#
对于那n对钥匙中的某对钥匙,有关系以及,表示用了K1就不能用K2,用了K2就不能用K1,对于每扇门也有相应的关系,如果不用其中的一把钥匙打开门,就必须用另一把钥匙打开另一扇门,就是关系以及,根据这些关系二分能开多少门,每次重新建图并check即可
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <set>
#include <vector>
#include <cctype>
#include <iomanip>
#include <sstream>
#include <climits>
#include <queue>
#include <stack>
using namespace std;
/* freopen("k.in", "r", stdin);
freopen("k.out", "w", stdout); */
// clock_t c1 = clock();
// std::cerr << "Time:" << clock() - c1 <<"ms" << std::endl;
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define de(a) cout << #a << " = " << a << endl
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef vector<int, int> VII;
#define inf 0x3f3f3f3f
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll MAXN = 4e3 + 7;
const ll MAXM = 1e6 + 7;
const ll MOD = 1e9 + 7;
const double eps = 1e-6;
const double pi = acos(-1.0);
int head[MAXN << 2];
struct Edge
{
int v, Next;
} e[MAXN << 2];
int dfn[MAXN << 1], low[MAXN << 1], sta[MAXN << 1], top = -1, vis[MAXN << 1];
int cnt = -1;
int num[MAXN << 1];
int tot;
int n, m;
int dep;
void add(int u, int v)
{
e[++cnt].v = v;
e[cnt].Next = head[u];
head[u] = cnt;
}
void init()
{
memset(vis, 0, sizeof(vis));
memset(dfn, 0, sizeof(dfn));
memset(head, -1, sizeof(head));
memset(low, 0, sizeof(low));
memset(num, 0, sizeof(num));
top = -1;
cnt = -1;
dep = 0;
tot = 0;
}
struct node
{
int k1, k2;
} Lock[MAXN], Key[MAXN];
void tarjan(int now)
{
dfn[now] = low[now] = ++dep;
sta[++top] = now;
vis[now] = 1;
for (int i = head[now]; ~i; i = e[i].Next)
{
int v = e[i].v;
if (!dfn[v])
{
tarjan(v);
low[now] = min(low[now], low[v]);
}
else if (vis[v])
low[now] = min(low[now], dfn[v]);
}
if (dfn[now] == low[now])
{
tot++;
while (sta[top] != now)
{
num[sta[top]] = tot;
vis[sta[top--]] = 0;
}
vis[sta[top]] = 0;
num[sta[top--]] = tot;
}
} //强连通分量的标号来得到反向的拓扑序
bool check(int x)
{
init();
for (int i = 1; i <= n; i++)
{
add(Key[i].k1, Key[i].k2 + 2 * n);
add(Key[i].k2, Key[i].k1 + 2 * n);
}
for (int i = 1; i <= x; i++)
{
add(Lock[i].k1 + 2 * n, Lock[i].k2);
add(Lock[i].k2 + 2 * n, Lock[i].k1);
}
for (int i = 0; i < (n << 1); i++)
if (!dfn[i])
tarjan(i);
for (int i = 0; i < (n << 1); i++)
if (num[i + 2 * n] == num[i])
return false;
return true;
}
int main()
{
while (~scanf("%d%d", &n, &m) && n + m)
{
for (int i = 1; i <= n; i++)
scanf("%d%d", &Key[i].k1, &Key[i].k2);
for (int i = 1; i <= m; i++)
scanf("%d%d", &Lock[i].k1, &Lock[i].k2);
int l = 0, r = m;
while (l < r)
{
int mid = (l + r + 1) / 2;
if (check(mid))
l = mid;
else
r = mid - 1;
}
printf("%d\n", l);
}
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 数据库服务器 SQL Server 版本升级公告
· C#/.NET/.NET Core技术前沿周刊 | 第 23 期(2025年1.20-1.26)
· 程序员常用高效实用工具推荐,办公效率提升利器!