现在的位置: 首页 > 综合 > 正文

poj 2296 Map Labeler 2-SAT+二分答案

2013年01月02日 ⁄ 综合 ⁄ 共 4517字 ⁄ 字号 评论关闭

Description

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

 

//

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=100000;
int V,E;//点数(1) 边数
struct edge//邻接表
{
    int t;
    int next;
};
int p[maxn];//表头节点
edge G[maxn];
int l;
void init()
{
    memset(p,-1,sizeof(p));
    l=0;
}
//添加边
void addedge(int u,int t)
{
    G[l].t=t;
    G[l].next=p[u];
    p[u]=l++;
}
//tarjan算法 求有向图强联通分量
int dfn[maxn],lowc[maxn];
//dfn[u]节点u搜索的次序编号,lowc[u]u或者u的子树能够追溯到的栈中的最早的节点
int belg[maxn];//第i个节点属于belg[i]个强连通分量
int stck[maxn],stop;//stck栈
int instck[maxn];//第i个节点是否在栈中
int scnt;//强联通分量
int index;
void dfs(int i)
{
    dfn[i]=lowc[i]=++index;
    instck[i]=1;//节点i入栈
    stck[++stop]=i;
    for(int j=p[i];j!=-1;j=G[j].next)
    {
        int t=G[j].t;
        //更新lowc数组
        if(!dfn[t])//t没有遍历过
        {
        dfs(t);
        if(lowc[i]>lowc[t]) lowc[i]=lowc[t];
        }//t是i的祖先节点
        else if(instck[t]&&lowc[i]>dfn[t]) lowc[i]=dfn[t];
    }
    //是强连通分量的根节点
    if(dfn[i]==lowc[i])
    {
    scnt++;
    int t;
    do
    {
        t=stck[stop--];
        instck[t]=0;
        belg[t]=scnt;
        }while(t!=i);
    }
}
int tarjan()
{
    stop=scnt=index=0;
    memset(dfn,0,sizeof(dfn));
    memset(instck,0,sizeof(instck));
    for(int i=1;i<=V;i++)
    {
        if(!dfn[i]) dfs(i);
    }
    return scnt;
}
int x[200],y[200];
int _abs(int x)
{
    return x>0?x:-x;
}
int main()
{
    int ci;scanf("%d",&ci);
    while(ci--)
    {
        int n;scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d%d",&x[i],&y[i]);
        int l=0,r=20000;
        while(l<r)
        {
            int mid=(l+r)>>1;
            V=2*n;init();
            //up <=n  down <=2*n
            for(int i=1;i<=n;i++)
            {
                for(int j=i+1;j<=n;j++)//important i+1
                {
                    if(_abs(x[i]-x[j])<mid)//发生冲突
                    {
                        if(y[i]==y[j])//只能一上一下
                        {
                            addedge(i,j+n);
                            addedge(i+n,j);
                            addedge(j,i+n);
                            addedge(j+n,i);
                        }
                        else if(_abs(y[i]-y[j])<mid)//一上一下,有y决定
                        {
                            if(y[i]>y[j])//i肯定上,j肯定下
                            {
                                addedge(i+n,i);
                                addedge(j,j+n);
                            }
                            else //i下,j上
                            {
                                addedge(i,i+n);
                                addedge(j+n,j);
                            }
                        }
                        else if(_abs(y[i]-y[j])<2*mid)//上下之间能放一个
                        {
                            if(y[i]>y[j])
                            {
                                addedge(i+n,j+n);
                                addedge(j,i);
                            }
                            else
                            {
                                addedge(j+n,i+n);
                                addedge(i,j);
                            }
                        }
                    }
                }
            }
            tarjan();
            int flag=1;
            for(int i=1;i<=n;i++)
            {
                if(belg[i]==belg[i+n])
                {
                    flag=0;break;
                }
            }
            if(!flag) r=mid;
            else l=mid+1;
        }
        printf("%d\n",l-1);
    }
    return 0;
}

抱歉!评论已关闭.