CF85 E Guard Towers——二分图
题目: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——二分图的更多相关文章
- CF 85E Guard Towers——二分图染色
题目:http://codeforces.com/contest/85/problem/E 当然是二分.然后连一个图,染色判断是不是二分图即可.方案数就是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 ...
- 「CF85E」 Guard Towers
「CF85E」 Guard Towers 模拟赛考了这题的加强版 然后我因为初值问题直接炸飞 题目大意: 给你二维平面上的 \(n\) 个整点,你需要将它们平均分成两组,使得每组内任意两点间的曼哈顿距 ...
- CF85E Guard Towers(二分答案+二分图)
题意 已知 N 座塔的坐标,N≤5000 把它们分成两组,使得同组内的两座塔的曼哈顿距离最大值最小 在此前提下求出有多少种分组方案 mod 109+7 题解 二分答案 mid 曼哈顿距离 >mi ...
- 【二分答案+贪心】UVa 1335 - Beijing Guards
Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the Imperial City ...
- LA 3177 Beijing Guards(二分法 贪心)
Beijing Guards Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the ...
- LA3177 Beijing Guards
Beijing Guards Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the ...
- BAPC K题 Keep Him Inside
Problem Statement: As a result of a long-standing war between the Sorcerers and the Orcs, you have b ...
- [Swift]LeetCode785. 判断二分图 | Is Graph Bipartite?
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
随机推荐
- Java图形界面GUI
Java图形界面GUI 设置窗体JFrame对象 package com.Aha.Best; import javax.swing.ImageIcon; import javax.swing.JFra ...
- Java中XML数据
Java中XML数据 XML解析——Java中XML的四种解析方式 XML是一种通用的数据交换格式,它的平台无关性.语言无关性.系统无关性.给数据集成与交互带来了极大的方便.XML在不同的语言环境中解 ...
- UVA - 12661 Funny Car Racing (Dijkstra算法)
题目: 思路: 把时间当做距离利用Dijkstra算法来做这个题. 前提:该结点e.c<=e.a,k = d[v]%(e.a+e.b); 当车在这个点的1处时,如果在第一个a这段时间内能够通过且 ...
- [BOI2008]Elect 选举
背包. #include <algorithm> #include <iostream> #include <cstdlib> #include <cstri ...
- java.lang unsupported classversion解决方法
设置编译的jdk和运行的jdk环境版本是否一致.一般都是jdk导致的.刚开始用jdk1.6编译运行,死活不行,换成jdk1.7运行也是1.7,ok
- buf.writeInt16BE()函数详解
buf.writeInt16BE(value, offset[, noAssert]) buf.writeInt16LE(value, offset[, noAssert]) value {Numbe ...
- unbuntu 安装软件
下载ubutun镜像---------------------用win32diskimager将镜像文件写入u盘,使用u盘启动安装系统. 安装软件--------------------- 0,基本工 ...
- 框架之---Flask
Flask是一个轻量级的WEB框架,它和Django相比,就是一个胖子和一个骨架的区别. Flask是一个py文件就可以run的web框架,但是就因为是py文件就能run,所有,啥都没有,等把Flas ...
- centos7安装:license information(license not accepted)
安装centos7的时候明明已经选择了默认的许可证信息,不知道哪里出错了,安装到最后,就会显示license information(license not accepted)的信息.解决方法如下: ...
- 通过混合编程分析的方法和机器学习预测Web应用程序的漏洞
通过混合编程分析的方法和机器学习预测Web应用程序的漏洞 由于时间和资源的限制,web软件工程师需要支持识别出有漏洞的代码.一个实用的方法用来预测漏洞代码可以提高他们安全审计的工作效率.在这篇文章中, ...