题目: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. 第三节:EF

    1.删除要进行判空 public ActionResult DelClassMethod(string gId) { //根据gId查询对应条目 var grade = oc.BllSession.I ...

  2. 最长上升子序列(动态规划递推,LIS)

    1759:最长上升子序列 题目: 总时间限制: 2000ms 内存限制: 65536kB 描述 一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的 ...

  3. 「 hihoCoder 1014 」Trie 树

    标题真直接 题目大意 给你 $n$ 个字符串.存到一个字典中.又给你 $m$ 个询问,每个询问给一个字符串,在字典中查出有多少个字符串是以这个字符串为前缀. 解题思路 模板题啊 在每个点设置一个变量 ...

  4. [Luogu] P4254 [JSOI2008]Blue Mary开公司

    题目背景 Blue Mary 最近在筹备开一家自己的网络公司.由于他缺乏经济头脑,所以先后聘请了若干个金融顾问为他设计经营方案. 题目描述 万事开头难,经营公司更是如此.开始的收益往往是很低的,不过随 ...

  5. db2层级查询

    CREATE VIEW v_orgtype99 asSELECT t1.SYS_ORG_TYPE_NAME top_name1, t2.SYS_ORG_TYPE_NAME top_name2, --若 ...

  6. Datatable 插入一行数据到第一行

    var t = $('#passwdHOST').DataTable({ 'searching': true, 'ordering': false, 'autoWidth': false, dom: ...

  7. java List 数组删除元素

    在 java 中,ArrayList 是一个很常用的类,在编程中经常要对 ArrayList 进行增.删.改.查操作.之前在学校时一直认为删除操作是最简单的,现在才越发觉得自己愚蠢.只需要设置好预期条 ...

  8. 51. spring boot属性文件之多环境配置【从零开始学Spring Boot】

    原本这个章节是要介绍<log4j多环境不同日志级别的控制的>但是没有这篇文章做基础的话,学习起来还是有点难度的,所以我们先一起了解下spring boot属性文件之多环境配置,当然文章中也 ...

  9. js cookies all in one

    js cookies all in one cookies // http://10.1.5.202/auto-deploy-platform/publish/index.html // 非当前 UR ...

  10. mtk display 架构

    Hwc Dispatch Layers  .Layer 的区分 每个layer 都会有对应的一个BufferQueue,BufferQueue都有一个mConnectapi属性,mConnectAPI ...