hdu1816Get Luffy Out *

来源:互联网 发布:雷神软件下载 编辑:程序博客网 时间:2024/06/04 23:21

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1816

题意:有2*n种钥匙,n对钥匙的约束(用了一把另一把那种钥匙就不能用了),m扇门。每扇门上有两把锁,打开一把就能打开门。问最多能打开多少扇门。

分析:求最大值我们可以二分,然后根据约束条件用2-SAT算法就行了。

代码:

#include<map>#include<set>#include<stack>#include<cmath>#include<queue>#include<bitset>#include<math.h>#include<vector>#include<string>#include<stdio.h>#include<cstring>#include<iostream>#include<algorithm>#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;typedef double db;typedef long long ll;typedef unsigned int uint;typedef unsigned long long ull;const db eps=1e-5;const int N=4e3+50;const int M=7e3+10;const ll MOD=1000000007;const int mod=1000000007;const int MAX=1000000010;const double pi=acos(-1.0);struct twosat {    int n,k,d[N];    bool mark[N];    int tot,u[N],v[M],pre[M];    void init(int a) {        n=a<<1;tot=0;        for (int i=0;i<n;i++) u[i]=-1,mark[i]=false;    }    void add_Edge(int a,int aval,int b,int bval) {        a=a*2+aval;b=b*2+bval;        v[tot]=b;pre[tot]=u[a^1];u[a^1]=tot++;        v[tot]=a;pre[tot]=u[b^1];u[b^1]=tot++;    }    bool dfs(int a) {        if (mark[a]) return true;        if (mark[a^1]) return false;        mark[a]=true;d[k++]=a;        for (int i=u[a];~i;i=pre[i])        if (!dfs(v[i])) return false;        return true;    }    bool solve() {        int i,j;k=0;        for (i=0;i<n;i+=2)        if (!mark[i]&&!mark[i^1]) {            if (!dfs(i)) {                for (j=0;j<k;j++) mark[d[j]]=false;                k=0;if (!dfs(i^1)) return false;            }        }        return true;    }}TS;int n,m,x[N],y[N];bool pd(int a) {    TS.init(2*n);    for (int i=1;i<=n;i++) TS.add_Edge(x[i],0,y[i],0);    for (int i=n+1;i<=n+a;i++) TS.add_Edge(x[i],1,y[i],1);    return TS.solve();}int main(){    int i,l,r,mid;    while (scanf("%d%d", &n, &m)&&(n+m)) {        for (i=1;i<=n+m;i++) scanf("%d%d", &x[i], &y[i]);        l=0;r=m+1;mid=(l+r)>>1;        while (l+1<r)        if (pd(mid)) l=mid,mid=(l+r)>>1;        else r=mid,mid=(l+r)>>1;        printf("%d\n", l);    }    return 0;}


0 0