POJ 2723 - Get Luffy Out 读清题意~简单二分2sat
来源:互联网 发布:共享办公软件 编辑:程序博客网 时间:2024/06/05 02:39
题意:
主人公在一个类似于塔结构的地方..每一层有一个门..门上有两个钥匙孔..只要有其中的一片钥匙就能打开门继续往上走了(一个门可能有两个相同的钥匙孔..也可能好几个门都有相同的钥匙孔..)..而所有的钥匙被分成了N个pair..每个pair中只能拿一把钥匙..现在问主人公最多能进入第几层...
题解:
值得注意的是必须通过了K层才能进入K+1层...这样就形成了一种单调关系...可以用二分来解决了..二分层数..然后2-sat判断...
Program:
#include<iostream>#include<stdio.h>#include<string.h>#include<cmath>#include<queue>#include<stack>#include<set>#include<time.h>#include<map>#include<algorithm>#define ll long long#define eps 1e-5#define oo 10007#define pi acos(-1.0)#define MAXN 5005<<1#define MAXM 5000005<<1using namespace std; struct node{ int y,next;}line[MAXM];vector<int> T[MAXN];int Lnum,_next[MAXN],key[MAXN][2],hash[MAXN],door[MAXN][2];int dfn[MAXN],low[MAXN],tp[MAXN],tpnum,DfsIndex;bool instack[MAXN];stack<int> mystack;void addline(int x,int y){ line[++Lnum].next=_next[x],_next[x]=Lnum,line[Lnum].y=y;}void tarjan(int x){ instack[x]=true,mystack.push(x); dfn[x]=low[x]=++DfsIndex; for (int k=_next[x];k;k=line[k].next) { int y=line[k].y; if (!dfn[y]) { tarjan(y); low[x]=min(low[x],low[y]); }else if (instack[y]) low[x]=min(low[x],dfn[y]); } if (low[x]==dfn[x]) { tpnum++; do { x=mystack.top(); mystack.pop(); instack[x]=false; tp[x]=tpnum; }while (low[x]!=dfn[x]); }}bool _2sat(int N,int M){ int i,m,k; for (i=0;i<(N<<1);i++) T[i].clear(); for (i=1;i<=M;i++) T[door[i][0]].push_back(door[i][1]), T[door[i][1]].push_back(door[i][0]); Lnum=0,memset(_next,0,sizeof(_next)); for (i=0;i<N;i++) { int x=key[i][0],y=key[i][1]; m=T[y].size(); for (k=0;k<m;k++) addline(i<<1,hash[T[y][k]]); m=T[x].size(); for (k=0;k<m;k++) addline(i<<1|1,hash[T[x][k]]); } memset(dfn,0,sizeof(dfn)); memset(instack,false,sizeof(instack)); while (!mystack.empty()) mystack.pop(); DfsIndex=tpnum=0; for (i=0;i<(N<<1);i++) if (!dfn[i]) tarjan(i); for (i=0;i<N;i++) if (tp[i<<1]==tp[i<<1|1]) return false; return true;}int main(){ int N,M,i; while (~scanf("%d%d",&N,&M) && N) { for (i=0;i<N;i++) { scanf("%d%d",&key[i][0],&key[i][1]); hash[key[i][0]]=i<<1,hash[key[i][1]]=i<<1|1; } for (i=1;i<=M;i++) scanf("%d%d",&door[i][0],&door[i][1]); int l=0,r=M+1,mid; while (r-l>1) { mid=l+r>>1; if (!_2sat(N,mid)) r=mid; else l=mid; } printf("%d\n",l); } return 0;}
- POJ 2723 - Get Luffy Out 读清题意~简单二分2sat
- [2-sat+二分]poj 2723 Get Luffy Out
- POJ 2723 Get Luffy Out(2-SAT + 二分)
- POJ 2723 Get Luffy Out 二分 2-SAT
- poj 2723 Get Luffy Out(2-sat + 二分)
- Poj 2723 Get Luffy Out (2-SAT + 二分)
- Get Luffy Out (poj 2723 二分+2-SAT)
- POJ 2723Get Luffy Out 二分 + 2-sat
- POJ 2723Get Luffy Out 2-SAT&&二分搜索
- |poj 2723|2-SAT|二分|Get Luffy Out
- poj 2723 Get Luffy Out 二分+2-SAT
- POJ 2723 Get Luffy Out(2-SAT)
- poj 2723 Get Luffy Out 2sat
- 2-sat->poj 2723 Get Luffy Out
- POJ 2723 Get Luffy Out(2-SAT)
- poj 2723 Get Luffy Out 2-SAT
- poj 2723 Get Luffy Out 2-sat
- POJ 2723 - Get Luffy Out(2-SAT)
- MVC2.0在Tab页中实现异步无刷新分页
- 精通 hibernate 孙卫琴 PDF 下载路径
- 授人以鱼,不如授人以渔
- Deep Learning论文笔记之(四)CNN卷积神经网络推导和实现
- 基于tiny6410的led驱动程序
- POJ 2723 - Get Luffy Out 读清题意~简单二分2sat
- hdu 1159 最长公共子序列
- 本人开发的第一款ios:比特币实时行情+挖矿收益计算器
- JForum 2.1.9安装后没有发帖图标的解决方法
- 飘逸的python - 对字典diff("异或")
- Eclipse的SVN插件与本地客户端关联不上如何解决
- 结构体数组初始化
- c语言字符串函数的实现
- 解决VS2010在程序根目录生成ipch文件夹和.sdf格式文件问题