夏夜、

心若平似镜、何题不AC。

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!='\n')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\n",l-1);
    }
    return 0;
}
View Code

 

posted on 2014-03-19 23:20  BMan、  阅读(171)  评论(0编辑  收藏  举报

导航