poj2723-Get Luffy Out

一道2-SAT问题,每对钥匙需要加一条边,每扇门上的对应的要用的钥匙加一条边。

其实求解2-SAT问题,关键在于找到不能同时成立的条件,例如在本题中,每对钥匙不能同时使用,每扇门上的钥匙不能同时不使用。

  1 #include<cstdio>
  2 #include<vector>
  3 #include<cstring>
  4 using namespace std;
  5
  6 const int maxn = 1<<12;
  7
  8 struct TwoSAT
  9 {
 10     int n;
 11     vector<int> G[maxn*2];
 12     bool mark[maxn*2];
 13     int S[maxn*2], c;
 14
 15     bool dfs(int x)
 16     {
 17         if (mark[x^1]) return false;
 18         if (mark[x]) return true;
 19         mark[x] = true;
 20         S[c++] = x;
 21         for (int i = 0; i < G[x].size(); i++)
 22             if (!dfs(G[x][i])) return false;
 23         return true;
 24     }
 25
 26     void init(int n)
 27     {
 28         this->n = n;
 29         for (int i = 0; i < n*2; i++) G[i].clear();
 30         memset(mark, 0, sizeof(mark));
 31     }
 32
 33     // x = xval or y = yval
 34     void add_clause(int x, int xval, int y, int yval)
 35     {
 36         x = x * 2 + xval;
 37         y = y * 2 + yval;
 38         G[x^1].push_back(y);
 39         G[y^1].push_back(x);
 40     }
 41
 42     bool solve()
 43     {
 44         for(int i = 0; i < n*2; i += 2)
 45             if(!mark[i] && !mark[i+1])
 46             {
 47                 c = 0;
 48                 if(!dfs(i))
 49                 {
 50                     while(c > 0) mark[S[--c]] = false;
 51                     if(!dfs(i+1)) return false;
 52                 }
 53             }
 54         return true;
 55     }
 56 };
 57
 58
 59 ///////////////////////////////////////////////////////////////
 60 #include <iostream>
 61 TwoSAT solver;
 62 typedef pair<int ,int > CPair;
 63 #define K1 first
 64 #define K2 second
 65 CPair doors[1<<12];
 66 CPair keys[1<<12];
 67 int n;
 68
 69
 70 bool test(int nd)
 71 {
 72     solver.init(2*n);
 73     // 重新构图
 74     // 加入钥匙
 75     for(int i=0;i<n;i++)
 76     {
 77         solver.add_clause(keys[i].K1,1,keys[i].K2,1); // 1表示使用key,0表示不使用
 78     }
 79     // 加入门
 80     for(int i=0;i<=nd;i++)
 81     {
 82         solver.add_clause(doors[i].K1,0,doors[i].K2,0); // 1表示使用key,0表示不使用
 83     }
 84     return solver.solve();
 85 }
 86
 87 int main()
 88 {
 89     #ifndef ONLINE_JUDGE
 90     freopen("in.txt","r",stdin);
 91     #endif
 92
 93     int nDoor;
 94     while(scanf("%d %d",&n,&nDoor),n+nDoor)
 95     {
 96         // 添加一对钥匙
 97         for(int i=0;i<n;i++)
 98         {
 99             scanf("%d %d",&keys[i].K1,&keys[i].K2);
100         }
101
102         // 门
103         for(int i=0;i<nDoor;i++)
104         {
105             scanf("%d %d",&doors[i].K1,&doors[i].K2);
106         }
107
108         int L=0,R=nDoor-1;
109         while(L < R)
110         {
111             int M = L + (R-L+1)/2;
112             if(test(M)) L = M;
113             else R = M-1;
114         }
115         printf("%d\n",L+1);
116     }
117
118     return 0;
119 }
时间: 2024-10-12 14:14:21

poj2723-Get Luffy Out的相关文章

图论专题整理

poj1251 Jungle Roads 思路:最小生成树          解题报告Here CodeForces 472D Design Tutorial: Inverse the Problem 思路:最小生成树          解题报告Here poj1789 Truck History 思路:最小生成树          解题报告Here poj1639 Picnic Planning 思路:顶点度数限制的MST         解题报告Here poj1062 昂贵的聘礼 思路:S

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

HDU 1816, POJ 2723 Get Luffy Out pid=1816" target="_blank" style="">题目链接 题意:N串钥匙.每串2把,仅仅能选一把.然后有n个大门,每一个门有两个锁,开了一个就能通过,问选一些钥匙,最多能通过多少个门 思路:二分通过个数.然后对于钥匙建边至少一个不选,门建边至少一个选,然后2-sat搞一下就可以. 一開始是按每串钥匙为1个结点,但是后面发现数据有可能一把钥匙,出如今不同串(真是不合

Get Luffy Out (poj 2723 二分+2-SAT)

Language: Default Get Luffy Out Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7969   Accepted: 3061 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 o

POJ2723-Get Luffy Out(2-SAT)

Get Luffy Out Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7456   Accepted: 2835 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 Arlo

HDU 1816 Get Luffy Out *

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

POJ 2723 Get Luffy Out(图论-2SAT,搜索-二分)

Get Luffy Out Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7488   Accepted: 2845 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 Arlo

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

(失败)配置vue+nginx+uwsgi luffy项目

2019-6-2 10:06:55 我咋配置 Nginx就是不能访问vue 也许里面还是有错误 也许是vue里面东西有改动把 等学会完vue理解完 也许就会配置啦!就好比自己的bbs项目! ...... 又发现自己的知识不足! 放上一些踩过的坑(没人知道就是有好多的坑 也许是这些坑可以让你更好的成长!!!!) 越努力,越幸运!永远不要高估自己! 现在开始准备四级然后  项目就是用本地luffy项目 做点好玩的东西吧! 我觉得要是面试的话 有个luffy项目 还是很吊的  顺便把luffy项目弄透

luffy前台案例

目录 luffy前台案例 1.课程点击跳转详情页 2.详情页的数据分析与布局 3.路由有名分组传参 4.route-link的两种传参方式 5.路由逻辑跳转与传参 6.axios安装及请求后台 7.vue-cookie的使用 8.element-ui的使用 luffy前台案例 1.课程点击跳转详情页 #views/Course.vue <template> <div class="course"> <Nav></Nav> <h1&g

luffy前期准备

一.pip 安装源 """ 1.采用国内源,加速下载模块的速度 2.常用pip源: -- 豆瓣:https://pypi.douban.com/simple -- 阿里:https://mirrors.aliyun.com/pypi/simple 3.加速安装的命令: -- >: pip install -i https://pypi.douban.com/simple 模块名 """ 永久配置安装源:Windows ""