题目:http://codeforces.com/contest/85/problem/E

给定一些点的坐标,求把它们分成两组,组内最大距离的最小值;

二分答案,判断就是看距离大于 mid 的点能否组成二分图,若能组成则可行,2^(连通块个数)就是方案数;

n^2 连边果然会超时...直接在 dfs 里判断距离就好了。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
int const maxn=,mod=1e9+;
int n,x[maxn],y[maxn],ans,col[maxn],cnt,hd[maxn],ct,num;
bool vis[maxn];
//struct N{
// int to,nxt;
// N(int t=0,int n=0):to(t),nxt(n) {}
//}ed[maxn*maxn];
//void add(int x,int y){ed[++ct]=N(y,hd[x]); hd[x]=ct;}
int calc(int i,int j){return abs(x[i]-x[j])+abs(y[i]-y[j]);}
bool dfs(int x,int mid)
{
vis[x]=;
// for(int i=hd[x],u;i;i=ed[i].nxt)
for(int u=;u<=n;u++)
{
if(calc(x,u)<=mid||u==x)continue;
if(vis[u])
{
if(col[u]==col[x])return ;
continue;
}
col[u]=!col[x];
if(dfs(u,mid))return ;
}
return ;
}
bool ck(int mid)
{
cnt=;
// ct=0;
// memset(hd,0,sizeof hd);
// memset(col,0,sizeof col);
memset(vis,,sizeof vis);
// for(int i=1;i<=n;i++)
// for(int j=i+1;j<=n;j++)
// if(calc(i,j)>mid)add(i,j),add(j,i);
for(int i=;i<=n;i++)
if(!vis[i])
{
if(dfs(i,mid))return ;
cnt++;
}
return ;
}
ll pw(ll a,int b)
{
ll ret=;
for(;b;b>>=,a=(a*a)%mod)
if(b&)ret=(ret*a)%mod;
return ret;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d%d",&x[i],&y[i]);
int l=,r=;
while(l<=r)
{
int mid=((l+r)>>);
if(ck(mid))ans=mid,num=cnt,r=mid-;
else l=mid+;
}
printf("%d\n%I64d\n",ans,pw(,num));
return ;
}

CF85 E Guard Towers——二分图的更多相关文章

  1. CF 85E Guard Towers——二分图染色

    题目:http://codeforces.com/contest/85/problem/E 当然是二分.然后连一个图,染色判断是不是二分图即可.方案数就是2^(连通块个数). 别真的连边!不然时间空间 ...

  2. [CF85E] Guard Towers - 二分+二分图

    题目描述 In a far away kingdom lives a very greedy king. To defend his land, he built n n n guard towers ...

  3. 「CF85E」 Guard Towers

    「CF85E」 Guard Towers 模拟赛考了这题的加强版 然后我因为初值问题直接炸飞 题目大意: 给你二维平面上的 \(n\) 个整点,你需要将它们平均分成两组,使得每组内任意两点间的曼哈顿距 ...

  4. CF85E Guard Towers(二分答案+二分图)

    题意 已知 N 座塔的坐标,N≤5000 把它们分成两组,使得同组内的两座塔的曼哈顿距离最大值最小 在此前提下求出有多少种分组方案 mod 109+7 题解 二分答案 mid 曼哈顿距离 >mi ...

  5. 【二分答案+贪心】UVa 1335 - Beijing Guards

    Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the Imperial City ...

  6. LA 3177 Beijing Guards(二分法 贪心)

    Beijing Guards Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the ...

  7. LA3177 Beijing Guards

    Beijing Guards Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the ...

  8. BAPC K题 Keep Him Inside

    Problem Statement: As a result of a long-standing war between the Sorcerers and the Orcs, you have b ...

  9. [Swift]LeetCode785. 判断二分图 | Is Graph Bipartite?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

随机推荐

  1. 解决移动端 手机号input 属性为 number,maxlength无效情况

    <input type="number" oninput="if(value.length>11)value=value.slice(0,11)" ...

  2. 字符串系列——KMP模板整理

    KMP模板整理 KMP与扩展KMP: /*vs 2017/ vs code以外编译器,去掉windows.h头文件和system("pause");*/ #include<i ...

  3. Python爬虫常用库安装

    建议更换pip源到国内镜像,下载会快很多:https://www.cnblogs.com/believepd/p/10499844.html requests pip3 install request ...

  4. Django REST framework 数据处理api

    一.url分发 以防有其他业务线的需要,导致url杂乱,将每个app用到的url都设置在自己的应用中. # 项目下的url url(r"^api/(?P<version>\w+) ...

  5. BZOJ 3747 洛谷 3582 [POI2015]Kinoman

    [题解] 扫描线+线段树. 我们记第i部电影上次出现的位置是$pre[i]$,我们从$1$到$n$扫描,每次区间$(pre[i],i]$加上第i部电影的贡献$w[f[i]]$,区间$[pre[pre[ ...

  6. hadoop balancer 二

    1.每次迭代一个datanode会移动不超过如下两者中较小的一个的数据量 1)10G 2)能力阈值 dfs.datanode.balance.bandwidthPerSec 每次迭代时间不会超过20分 ...

  7. java方法的虚分派和方法表

    java:方法的虚分派(virtual dispatch)和方法表(method table) Java方法调用的虚分派 虚分配(Virtual Dispatch) 首先从字节码中对方法的调用说起.J ...

  8. 由MTK平台 mtkfb 设备注册疑问引发的知识延伸--ARM Device Tree

    问题: 在kernel-3.10\drivers\misc\mediatek\videox\mt6735\mtkfb.c里面int __init mtkfb_init(void) 有看到 platfo ...

  9. 洛谷—— P1339 [USACO09OCT]热浪Heat Wave

    P1339 [USACO09OCT]热浪Heat Wave 题目描述 The good folks in Texas are having a heatwave this summer. Their ...

  10. jd-eclipse插件的安装

    一,资源 jd-eclipse-site-1.0.0-RC2.zip    百度网盘链接:https://pan.baidu.com/s/1GTFFY_1jg4k9vjZNE4JliQ       提 ...