二分枚举答案,2-SAT检验是否可行。
#include <bits/stdc++.h>
using namespace std;
const int N=2e3+5;
int n,l,r,mid,ans,tot,a[N*2];
struct number{
int x,y;}num[N];
int now,top,col,dfn[N*2],low[N*2],sta[N*2],color[N*2];
int cnt,head[N*2];
struct edge{
int next,to;}e[N*N*4];
inline void add(int u,int v)
{
cnt++;
e[cnt].next=head[u];
e[cnt].to=v;
head[u]=cnt;
}
void tarjan(int u)
{
dfn[u]=low[u]=++now;
sta[++top]=u;
for (register int i=head[u]; i; i=e[i].next)
{
if (!dfn[e[i].to])
{
tarjan(e[i].to);
low[u]=min(low[u],low[e[i].to]);
}
else if (!color[e[i].to]) low[u]=min(low[u],low[e[i].to]);
}
if (dfn[u]==low[u])
{
col++;
while (sta[top]!=u) color[sta[top]]=col,top--;
color[sta[top]]=col,top--;
}
}
inline bool jay(int mid)
{
cnt=0; for (register int i=1; i<=2*n; ++i) head[i]=0;
for (register int i=1; i<=n; ++i)
{
for (register int j=1; j<=n; ++j)
if (i!=j)
{
if (abs(num[i].x-num[j].x)<mid) add(i,j+n);
if (abs(num[i].x-num[j].y)<mid) add(i,j);
if (abs(num[i].y-num[j].x)<mid) add(i+n,j+n);
if (abs(num[i].y-num[j].y)<mid) add(i+n,j);
}
}
now=top=col=0;
for (register int i=1; i<=2*n; ++i) dfn[i]=low[i]=color[i]=0;
for (register int i=1; i<=2*n; ++i) if (!dfn[i]) tarjan(i);
for (register int i=1; i<=n; ++i) if (color[i]==color[i+n]) return false;
return true;
}
int main(){
while (~scanf("%d",&n))
{
for (register int i=1; i<=n; ++i) scanf("%d%d",&num[i].x,&num[i].y);
l=0; r=1e7; ans=0;
while (l<=r)
{
mid=l+r>>1;
if (jay(mid)) ans=mid,l=mid+1;
else r=mid-1;
}
printf("%d\n",ans);
}
return 0;
}