题目: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. 虚拟机找不到本机vmnet0,vmnet8,无法连接xshell,解决方案

    首先出现这个问题肯定是第一次下载虚拟机把之前的注册表覆盖了,网卡找不到,首先卸载VMware 并且将C\ProgramData下的VMware文件夹删除掉 ,下载cceaner,点击注册表清除干净,再 ...

  2. wpf 自定义单选按钮 RadioButton

    新建RadioButtonEx.cs public class RadioButtonEx : RadioButton { public Geometry SelectIcon { get { ret ...

  3. Python 字符串常见的用法

    line = “ni hao wo jiao key” line.capotalize()#首字母大写 line.center(20)#居中显示固定的字符 line.count('n')#计数,计算该 ...

  4. 个人总结的常用java,anroid网站

    http://blog.csdn.net/wanghao200906/article/details/49334987

  5. vue父子通信的基本使用

    项目中没怎么用过父子通信,很多页面都是路由切换实现的,后来做首页的时候发现首页的路径没法配置,我强行在原先的首页上写了个子组件,通过判断路径使其强行跳转实现的 这个时候跳转页面的时候就要使用到了父子间 ...

  6. console.log格式化及console对象

    一.console.log格式化打印 console.log格式化这一用法一般都在个人博客或其他官网上有,当F12查看网页元素时,在控制台(console)那里偶尔会发现一些个性化的输出,感觉很奇特很 ...

  7. 透彻分析C/C++中memset函数

    在C语言中,经常需要对内存进行操作,里面涉及很多函数,但是memset函数的使用有一点需要大家格外注意,这也是我在做项目时遇到过的一个问题,调试了很久才找出来错误. 函数原型是:void *memse ...

  8. [Luogu] P1407 [国家集训队]稳定婚姻

    题目描述 我国的离婚率连续7年上升,今年的头两季,平均每天有近5000对夫妇离婚,大城市的离婚率上升最快,有研究婚姻问题的专家认为,是与简化离婚手续有关. 25岁的姗姗和男友谈恋爱半年就结婚,结婚不到 ...

  9. git 安装 使用

    git 安装--------------------------------------yum install git -y git 下载项目----------------------------- ...

  10. 51NOD 1154 回文串的划分(DP)

    思路:参考了网上,思路很清奇,借助vis[i][j]来表示从i到j是否为回文串,回文串这边是用的双重循环来写的:dp[i]用来表示以i结尾的字符串最少的回文串有多长. #include<cstr ...