zoukankan      html  css  js  c++  java
  • UVA 1146 Now or later 2-sat

    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<iostream>
    #include<sstream>
    #include<cmath>
    #include<climits>
    #include<string>
    #include<map>
    #include<queue>
    #include<vector>
    #include<stack>
    #include<set>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int,int> pii;
    #define pb(a) push(a)
    #define INF 0x1f1f1f1f
    #define lson idx<<1,l,mid
    #define rson idx<<1|1,mid+1,r
    #define PI  3.1415926535898
    template<class T> T min(const T& a,const T& b,const T& c) {
        return min(min(a,b),min(a,c));
    }
    template<class T> T max(const T& a,const T& b,const T& c) {
        return max(max(a,b),max(a,c));
    }
    void debug() {
    #ifdef ONLINE_JUDGE
    #else
    
        freopen("d:\in1.txt","r",stdin);
        freopen("d:\out1.txt","w",stdout);
    #endif
    }
    int getch() {
        int ch;
        while((ch=getchar())!=EOF) {
            if(ch!=' '&&ch!='
    ')return ch;
        }
        return EOF;
    }
    
    struct Edge
    {
        int u,v;
    };
    
    const int maxn=2005;
    
    struct TwoSat
    {
        vector<int> g[maxn*2];
        int mark[maxn*2];
        int s[maxn],c;
        int n;
        void init(int n)
        {
            this->n=n;
            for(int i=0;i<2*n;i++)
                g[i].clear();
            memset(mark,0,sizeof(mark));
        }
        void add_clause(int x,int xval,int y,int yval)
        {
            x=x*2+xval;
            y=y*2+yval;
            g[x].push_back(y^1);
            g[y].push_back(x^1);
        }
    
        bool dfs(int x)
        {
            if(mark[x^1])return false;
            if(mark[x])return true;
    
            mark[x]=1;
            s[c++]=x;
    
            for(int i=0;i<g[x].size();i++)
            {
                if(!dfs(g[x][i]))return false;
            }
            return true;
        }
        bool solve()
        {
            for(int i=0;i<n;i++)
            {
                int a=i*2,b=i*2+1;
                if(!mark[a]&&!mark[b])
                {
                    c=0;
                    if(!dfs(a))
                    {
                        while(c)mark[s[--c]]=false;
                        if(!dfs(b))return false;
                    }
                }
            }
            return true;
        }
    };
    
    TwoSat solver;
    int T[maxn][2];
    int n;
    void input(int n)
    {
        for(int i=0;i<n;i++)
            for(int a=0;a<2;a++)
                scanf("%d",&T[i][a]);
    }
    
    bool check(int t)
    {
        solver.init(n);
        for(int i=0;i<n;i++)for(int a=0;a<2;a++)
            for(int j=i+1;j<n;j++)for(int b=0;b<2;b++)
                if(abs(T[i][a]-T[j][b])<t)
                    solver.add_clause(i,a,j,b);
        return solver.solve();
    }
    int main()
    {
    
        while(scanf("%d",&n)!=EOF)
        {
            input(n);
            int l=0,r=10000001;
            while(l<r)
            {
                int mid=l+(r-l)/2;
                if(check(mid))
                    l=mid+1;
                else r=mid;
            }
            printf("%d
    ",l-1);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    delphi SysErrorMessage 函数和系统错误信息表 good
    Delphi中ShellExecute使用详解(详细解释10种显示状态)
    几个获取Windows系统信息的Delphi程序
    判断操作系统多久没有任何操作
    MVC4+WebApi+Redis Session共享练习(下)
    MVC4+WebApi+Redis Session共享练习(上)
    EntityFramework使用总结(与MVC4.0实现CURD操作)
    MVC3.0+knockout.js+Ajax 实现简单的增删改查
    基于Redis缓存的Session共享(附源码)
    Redis缓存服务搭建及实现数据读写
  • 原文地址:https://www.cnblogs.com/BMan/p/3612610.html
Copyright © 2011-2022 走看看