poj2296 2-sat加二分

Map generation is a difficult task in cartography. A vital part of such task is automatic labeling of the cities in a map; where for each city there is text label to be attached to its location, so that no two labels overlap. In this problem, we are concerned with a simple case of automatic map labeling. 

Assume that each city is a point on the plane, and its label is a text bounded in a square with edges parallel to x and y axis. The label of each city should be located such that the city point appears exactly in the middle of the top or bottom edges of the label. In a good labeling, the square labels are all of the same size, and no two labels overlap, although they may share one edge. Figure 1 depicts an example of a good labeling (the texts of the labels are not shown.) 

Given the coordinates of all city points on the map as integer values, you are to find the maximum label size (an integer value) such that a good labeling exists for the map. 

Input

The first line contains a single integer t (1 <= t <= 10), the number of test cases. Each test case starts with a line containing an integer m (3 ≤ m ≤ 100), the number of cities followed by m lines of data each containing a pair of integers; the first integer (X) is the x and the second one (Y) is the y coordinates of one city on the map (-10000 ≤X, Y≤ 10000). Note that no two cities have the same (x, y) coordinates.

Output

The output will be one line per each test case containing the maximum possible label size (an integer value) for a good labeling.

Sample Input

1
6
1 1
2 3
3 2
4 4
10 4
2 5

Sample Output

2

思路:每个点可以向上画正方形,也可以向下画,考虑这两种状态,向上画设为0,向下设为1,就可以用2sat求解,二分边长,枚举边,当i和j的横坐标小于mid时,如果它们纵坐标相同就必须忘相反方向画,如果相差小于mid,纵坐标大的不能往下画,纵坐标小的不能往上画,如果小于2倍mid,只有一种情况矛盾,就是上面往下同时下面往上,对应加边即可,详见代码。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define maxn 1005
using namespace std;
int n;
int t;
struct point
{
    int x,y;
}p[maxn];
struct twosat
{
    int n;
    vector<int>g[maxn];
    bool mark[maxn*2];
    int s[maxn*2];
    int c;
    void init(int n)
    {
        this->n=n;
        for(int i=0;i<2*n;i++)
            g[i].clear();
        memset(mark,0,sizeof(mark));
    }
    void addedge(int x,int xval,int y,int yval)
    {
        x=x*2+xval;
        y=y*2+yval;
        g[x].push_back(y);

    }
    bool dfs(int x)
    {
        if(mark[x^1])
            return false;
        if(mark[x])
            return true;
        mark[x]=true;
        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<2*n;i+=2)
            if(!mark[i]&&!mark[i+1])
        {
            c=0;
        if(!dfs(i))
           {
            while(c>0)
           mark[s[--c]]=false;
           if(!dfs(i+1))
            return false;
        }
        }
        return true;
    }
}TS;
bool check(int mid)
{TS.init(n);
    for(int i=0;i<n;i++)
    for(int j=i+1;j<n;j++)
        if(abs(p[i].x-p[j].x)<mid)
    {
        if(p[i].y==p[j].y)
        {
            TS.addedge(i,0,j,1);
            TS.addedge(i,1,j,0);
            TS.addedge(j,0,i,1);
            TS.addedge(j,1,i,0);
        }
        else if(abs(p[i].y-p[j].y)<mid)
        {
            if(p[i].y>p[j].y)
            {
                TS.addedge(i,1,i,0);
                TS.addedge(j,0,j,1);
            }
            else
            {
                TS.addedge(i,0,i,1);
                TS.addedge(j,1,j,0);
            }
        }
        else if(abs(p[i].y-p[j].y)<2*mid)
        {if(p[i].y>p[j].y)
            {TS.addedge(i,1,j,1);
            TS.addedge(j,0,i,0);
        }
        else
        {
            TS.addedge(j,1,i,1);
            TS.addedge(i,0,j,0);
        }
    }
    }
    return TS.solve();

}
int main()
{ scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);

        for(int i=0;i<n;i++)
            scanf("%d%d",&p[i].x,&p[i].y);
        int l=0,r=20005;
        while(l<r)
        {
            int mid=l+(r-l+1)/2;
            if(check(mid))
                l=mid;
            else
                r=mid-1;
        }
        printf("%d\n",l);
    }


return 0;

}

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/88087515