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

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;
}

發佈了743 篇原創文章 · 獲贊 5 · 訪問量 69萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章