题目: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. bazel和TensorFlow安装

    bazel安装:https://docs.bazel.build/versions/master/install-ubuntu.html#install-with-installer-ubuntu 安 ...

  2. linux下C/C++程序的内存布局

    内核空间和用户空间: 我们在编写程序时使用的内存空间叫虚拟内存,程序在运行时,要完成虚拟内存到物理内存的转换.假如在32位环境上,理论上我们可以使用的虚拟内存空间是4GB,但实际上这4GB并不能完全给 ...

  3. 本地搭建easy-mock

    easy-mock要用nodejs启动,需要先安装nodejs ubuntu系统: apt install node centos系统: curl --silent --location https: ...

  4. Go:变量、常量、枚举

    一.变量 package main import "fmt" func variableZeroValue() { // 变量声明 var a int var b string f ...

  5. subprocess操作命令

    import subprocess 一. run()方法 --->括号里面传参数,主要有cmd, stdout, shell, encoding, check 1.直接传命令 2.命令带参数要以 ...

  6. python3.x Day1 菜单程序练习

    三级菜单: 1. 运行程序输出第一级菜单 2. 选择一级菜单某项,输出二级菜单,同理输出三级菜单 3. 菜单数据保存在文件中 4. 让用户选择是否要退出 5. 有返回上一级菜单的功能 类定义:menu ...

  7. ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer

    传送门:https://nanti.jisuanke.com/t/31462 本题是一个树上的问题:结点间路径问题. 给定一个有N×M个结点的网格,并给出结点间建立墙(即拆除边)的代价.花费最小的代价 ...

  8. JavaScript及Java对JSON的相关处理

    JavaScript中JSON字符串与JSON对象的互转及JSON对象的取值: var jsonString = '{"key1":"value1"," ...

  9. 【hdu 1043】Eight

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...

  10. HDU 1561 树形DP背包问题

    这是自己第一道背包上树形结构问题,不是很理解这个概念的可以先看看背包九讲 自己第一次做,看了一下别人的思路,结合着对简单背包问题的求解方式自己一次AC了还是有点小激动的 题目大意是: 攻克m个城市,每 ...