Map Labeler POJ - 2296(2 - sat 具体关系建边)

时间:2023-03-08 17:45:39

题意:

  给出n个点  让求这n个点所能建成的正方形的最大边长,要求不覆盖,且这n个点在正方形上或下边的中点位置

解析:

  当然是二分,但建图就有点还行。。比较难想。。行吧。。。我太垃圾。。。

  2 - sat建图 一看逻辑关系,二看具体情况

  这里既有平常常用的逻辑关系,也有一些具体的关系

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define pd(a) printf("%d\n", a);
#define plld(a) printf("%lld\n", a);
#define pc(a) printf("%c\n", a);
#define ps(a) printf("%s\n", a);
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 1e5 + , INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
int n;
vector<int> G[maxn];
int sccno[maxn], vis[maxn], low[maxn], scc_cnt, scc_clock;
stack<int> S;
void init()
{
for(int i = ; i < maxn; i++) G[i].clear();
mem(sccno, );
mem(vis, );
mem(low, );
scc_cnt = scc_clock = ;
} struct node
{
int x, y;
}Node[maxn]; void dfs(int u)
{
vis[u] = low[u] = ++scc_clock;
S.push(u);
for(int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
if(!vis[v])
{
dfs(v);
low[u] = min(low[u], low[v]);
}
else if(!sccno[v])
low[u] = min(low[u], vis[v]);
}
if(low[u] == vis[u])
{
scc_cnt++;
for(;;)
{
int x = S.top(); S.pop();
sccno[x] = scc_cnt;
if(x == u) break;
}
}
} void build(int mid)
{
for(int i = ; i < n; i++)
for(int j = i + ; j < n; j++)
{
if(abs(Node[i].x - Node[j].x) >= mid) continue;
if(abs(Node[i].y - Node[j].y) >= * mid) continue;
if(abs(Node[i].y - Node[j].y) >= mid)
{
if(Node[i].y > Node[j].y) //i在下面的时候 j只能在下面
G[i << ].push_back(j << ), G[j << | ].push_back(i << | );
else
G[j << ].push_back(i << ), G[i << | ].push_back(j << | );
}
else if(abs(Node[i].y - Node[j].y) < mid)
{
if(Node[i].y > Node[j].y) //i只能在上面 j只能在下面
{
G[i << | ].push_back(j << );
G[j << ].push_back(i << | );
G[i << ].push_back(i << | );
G[j << | ].push_back(j << );
}
else if(Node[i].y < Node[j].y) //j只能在上面 i只能在下面
{
G[j << | ].push_back(i << );
G[i << ].push_back(j << | );
G[j << ].push_back(j << | );
G[i << | ].push_back(i << );
}
else //一个在上面 一个在下面
{
G[i << ].push_back(j << | );
G[j << | ].push_back(i << );
G[i << | ].push_back(j << );
G[j << ].push_back(i << | );
}
}
}
} bool check()
{
for(int i = ; i < n * ; i += )
if(sccno[i] == sccno[i + ])
return false;
return true;
} int main()
{
int T;
rd(T);
while(T--)
{
rd(n);
for(int i = ; i < n; i++)
rd(Node[i].x), rd(Node[i].y);
int l = , r = + , ans;
while(l <= r)
{
int mid = (l + r) / ;
init();
build(mid);
for(int i = ; i < n * ; i++) if(!vis[i]) dfs(i);
if(check()) l = mid + ;
else r = mid - ;
}
pd(r); } return ;
}