标签:des style http color os io strong for
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 7488 | Accepted: 2845 |
Description
Input
Output
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
Source
题目大意:
解题思路:有n对钥匙,m个门,每对钥匙用了其中1个,另一个就会消失,每个门上有m个锁,用特定的钥匙打开其中1个锁,另一个锁会消失,连续的打开门,问你之多能打开几扇门?
解题代码:二分枚举打开的门数,再用2SAT判断是否矛盾。‘
2SAT构边说明:两边分别钥匙,为选与不选
(1)AB钥匙在一串,那么选了A钥匙,就不能选B钥匙;选了B钥匙,就不能选A钥匙
(2)AB是同一扇门的锁,那么不开A就要开B,不开B就要开A。
#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=4100;
struct edge{
int u,v,next;
edge(int u0=0,int v0=0){
u=u0,v=v0;
}
}e[maxn*16];
int n,m,head[maxn],cnt;
int dfn[maxn],low[maxn],color[maxn],index,nc;
bool mark[maxn];
vector <edge> va,vb;
vector <int> vec;
void adde(int u,int v){
e[cnt]=edge(u,v),e[cnt].next=head[u],head[u]=cnt++;
}
void init(){
vec.clear();
index=nc=cnt=0;
for(int i=0;i<=4*n;i++){
dfn[i]=0;
color[i]=head[i]=-1;
mark[i]=false;
}
}
void input(){
va.clear();
vb.clear();
va.resize(n);
vb.resize(m);
for(int i=0;i<n;i++) scanf("%d%d",&va[i].u,&va[i].v);
for(int i=0;i<m;i++) scanf("%d%d",&vb[i].u,&vb[i].v);
}
void build(int r){
init();
for(int i=0;i<n;i++){
adde(va[i].u,va[i].v+2*n);
adde(va[i].v,va[i].u+2*n);
}
for(int i=0;i<=r;i++){
adde(vb[i].u+2*n,vb[i].v);
adde(vb[i].v+2*n,vb[i].u);
}
}
void tarjan(int s){
dfn[s]=low[s]=++index;
mark[s]=true;
vec.push_back(s);
for(int i=head[s];i!=-1;i=e[i].next){
int d=e[i].v;
if(!dfn[d]){
tarjan(d);
low[s]=min(low[s],low[d]);
}else if(mark[d]){
low[s]=min(low[s],dfn[d]);
}
}
if(low[s]==dfn[s]){
int d;
nc++;
do{
d=vec.back();
vec.pop_back();
color[d]=nc;
mark[d]=false;
}while(s!=d);
}
}
bool judge(int r){
build(r);
for(int i=0;i<4*n;i++){
if(!dfn[i]) tarjan(i);
}
for(int i=0;i<2*n;i++){
if(color[i]==color[i+2*n]) return false;
}
return true;
}
void solve(){
if(judge(m-1)){
printf("%d\n",m);
return;
}
int l=0,r=m-1;
while(l<r){
int mid=(l+r)/2;
if(judge(mid)) l=mid+1;
else r=mid;
}
printf("%d\n",r);
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF && (m||n) ){
input();
solve();
}
return 0;
}
POJ 2723 Get Luffy Out(图论-2SAT,搜索-二分),布布扣,bubuko.com
POJ 2723 Get Luffy Out(图论-2SAT,搜索-二分)
标签:des style http color os io strong for
原文地址:http://blog.csdn.net/a1061747415/article/details/38439407