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 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 <= 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 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

今天学习了 2-sat问题 这是一个现实中经常遇到的问题可以转化为图论去解决

  有m扇门,每扇门都有两把锁,只要打开其中一把就可以打开这扇门,

  打开了第i扇门才能继续开第i+1扇门。

  现在有2n个钥匙,每两个绑在一起,使用了一个就不能使用另一个。

  问最多能开几扇门。

 

  这个就是 2-sat问题的模板题

  以下是这类问题的基本的解题思路

 

  1. 构造有向图,这个有向图是一个表示了选择了一个点,一定要选择它能够到达的点的图。
  2. 对这个图求强连通图。
  3. 假如有两个点处在同一个强连通图内,这两个点是必须只能选择一个的一组内的点,就不可以。
  4. 否则可以

 

  a->b+2*n 表示 选a不选b

  门的建边就要 a+2*n->b 

  然后判断 a和a+2*n 在不在同一个强连通分量内

  回到题目来 这题的话网上普遍二分解决 因为这样快啊

  

  

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <vector>
 5 #include <string>
 6 #include <algorithm>
 7 #include <queue>
 8 #include <stack>
 9 
10 using namespace std;
11 const int maxn = 1e5 + 10;
12 const int  mod = 1e9 + 7 ;
13 const int INF = 0x7ffffff;
14 struct node {
15     int v, next;
16 } edge[maxn];
17 int head[maxn], dfn[maxn], low[maxn];
18 int s[maxn], belong[maxn], instack[maxn];
19 int tot, cnt, top, flag, n, m;
20 void init() {
21     tot = cnt = top = flag = 0;
22     memset(s, 0, sizeof(s));
23     memset(head, -1, sizeof(head));
24     memset(dfn, 0, sizeof(dfn));
25     memset(instack, 0, sizeof(instack));
26 }
27 void add(int u, int v ) {
28     edge[tot].v = v;
29     edge[tot].next = head[u];
30     head[u] = tot++;
31 }
32 void tarjin(int v) {
33     dfn[v] = low[v] = ++flag;
34     instack[v] = 1;
35     s[top++] = v;
36     for (int i = head[v] ; ~i ; i = edge[i].next ) {
37         int j = edge[i].v;
38         if (!dfn[j]) {
39             tarjin(j);
40             low[v] = min(low[v], low[j]);
41         } else if (instack[j]) low[v] = min(low[v], dfn[j]);
42     }
43     if (dfn[v] == low[v]) {
44         cnt++;
45         int t;
46         do {
47             t = s[--top];
48             instack[t] = 0;
49             belong[t] = cnt;
50         } while(t != v) ;
51     }
52 }
53 struct p {
54     int u, v;
55 } qu[maxn];
56 int check(int mid) {
57     init();
58     for (int i = 0 ; i < n ; i++) {
59         add(qu[i].v, qu[i].u + 2 * n);
60         add(qu[i].u, qu[i].v + 2 * n);
61     }
62     for (int i = 0 ; i < mid ; i++ ) {
63         add(qu[i + n].u + 2 * n, qu[i+n].v);
64         add(qu[i + n].v + 2 * n, qu[i+n].u);
65     }
66     for (int i = 0 ; i < 4 * n ; i++)
67         if (!dfn[i]) tarjin(i);
68     for (int i = 0 ; i < 2 * n ; i++)
69         if (belong[i] == belong[i + 2 * n]) return 0;
70     return 1;
71 }
72 int main() {
73     while(scanf("%d%d", &n, &m) != EOF) {
74         if (n == 0 && m == 0  ) break;
75         for (int i = 0 ; i < n ; i++)
76             scanf("%d%d", &qu[i].u, &qu[i].v);
77         for (int i = 0 ; i < m ; i++)
78             scanf("%d%d", &qu[i + n].u, &qu[i + n].v);
79         int low = 0, high = m, mid;
80         while(low <= high) {
81             mid = (low + high) / 2;
82             if (check(mid)) low = mid + 1;
83             else high = mid - 1;
84         }
85         printf("%d\n", high);
86     }
87     return 0;
88 }

 

转载于:https://www.cnblogs.com/qldabiaoge/p/9092918.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值