poj 2296: Map Labeler(二分答案 + 2-sat )

www.cnblogs.com/shaokele/


Map Labeler##

  Time Limit: 1 Sec
  Memory Limit: 64 MB

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.
  p1
  

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
  

题目地址:  poj 2296

题目大意:

  题目已经很简洁了>_<
  地图上有 n 个城市,每个城市需要贴上一个标签,所有标签是大小相同且不重叠的正方形,其对于的城市位于标签上边或下边的中点,最大化标签的边长

题解:

  二分+2-sat 裸题
  二分答案建图
  2-sat 判是否可行即可


AC代码

#include <cstdio> 
#include <cstring>
#include <algorithm>
using namespace std;
const int N=105;
int Q,n,cnt;
int x[N],y[N];
int last[N<<1];
struct edge{
	int to,next;
}e[(N*N)<<2];
void add_edge(int u,int v){
	e[++cnt]=(edge){v,last[u]};last[u]=cnt;
}
int ind,top,tot;
int q[N<<1],dfn[N<<1],low[N<<1],col[N<<1];
bool inq[N<<1];
void Tarjan(int u){
    dfn[u]=low[u]=++ind;
    q[++top]=u;inq[u]=1;
    for(int i=last[u];i;i=e[i].next){
        int v=e[i].to;
        if(!dfn[v]){
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(inq[v])
            low[u]=min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u]){
        tot++;
        int now=0;
        while(now!=u){
            now=q[top--];
            col[now]=tot;
            inq[now]=0;
        }
    }
}
bool ok(int d){
	cnt=ind=top=tot=0;
	memset(inq,0,sizeof(inq));
	memset(last,0,sizeof(last));
	memset(col,0,sizeof(col));
	memset(dfn,0,sizeof(dfn));
	for(int i=1;i<n;i++)
		for(int j=i+1;j<=n;j++){
			if(abs(x[i]-x[j])>=d)continue;
			int a=i,b=j;
			if(y[a]<y[b])
				swap(a,b);
			if(y[a]-y[b]>=2*d)continue;
			if(d<=y[a]-y[b] && y[a]-y[b]<2*d){
				add_edge(a*2,b*2);
				add_edge(b*2-1,a*2-1);
			}
			if(y[a]-y[b]<d)
				if(y[a]==y[b]){
					add_edge(a*2-1,b*2);
					add_edge(b*2,a*2-1);
					add_edge(a*2,b*2-1);
					add_edge(b*2-1,a*2);
				}else{
					add_edge(a*2,a*2-1);
					add_edge(b*2-1,b*2);
				}
		}
	for(int i=1;i<=n*2;i++) 
		if(!dfn[i])Tarjan(i);
	for(int i=1;i<=n;i++)
		if(col[i*2-1]==col[i*2])return 0;
	return 1;
}
int main(){
	scanf("%d",&Q);
	while(Q--){
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
			scanf("%d%d",&x[i],&y[i]);
		int l=0,r=20000,ans=-1;
		while(l<=r){
			int mid=(l+r)>>1;
			if(ok(mid)){
				ans=mid;
				l=mid+1;
			}else r=mid-1;
		}
		printf("%d
",ans);
	}
	return 0;
}
原文地址:https://www.cnblogs.com/shaokele/p/9455374.html