CF85E Guard Towers 二分+二分图染色

题意:
在这里插入图片描述
思路:

  1. 最大值最小二分哈曼顿距离建边
  2. 二分图01染色判断是否合理
    代码:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define forn(i,n) for(int i=0;i<n;i++)
#define for1(i,n) for(int i=1;i<=n;i++)
#define IO ios::sync_with_stdio(false);cin.tie(0)
const int mod = 1e9+7;
const int maxn = 5005;
int color[maxn],n,ans,res;
pair<int,int>a[maxn];
ll powmod(ll a,ll b){
    ll res = 1;
    for(;b;b>>=1){
        if(b&1) (res*=a)%=mod;
        (a*=a)%=mod;
    }
    return res;
}
bool dfs(int u,int len,bool cur){
    color[u] = cur;
    forn(i,n){
        if(u==i) continue;
        int v = abs(a[i].first-a[u].first)+abs(a[i].second-a[u].second);
        if(v<=len) continue;
        if(color[i]==-1) {
            if(!dfs(i,len,cur^1)) return 0;
        }
        else if(color[i]==cur) return 0;
    }
    return 1;
}
bool check(int len){
    forn(i,n) color[i] = -1;
    int cnt = 0;
    forn(i,n)if(color[i]==-1){
        cnt++;
        if(!dfs(i,len,0)) return 0;
    }
    ans = len,res= cnt;
    return 1;
}

int main(){
    IO;cin>>n;
    forn(i,n) cin>>a[i].first>>a[i].second;
    int l = 0,r = 1e4;
    while(l<=r){
        int mid = l+r>>1;
        if(check(mid)) r = mid-1;
        else l = mid+1;
    }
    res = powmod(2,res);
    cout << ans <<'
'<<res;
    return 0;
}
原文地址:https://www.cnblogs.com/AlexPanda/p/12520283.html