[ZOJ1482]Partitions
[ZOJ1482]Partitions
题目大意:
给定一个\(n\times n(n\le3000)\)的\(\texttt 0/\texttt1\)矩阵,求去掉所有的\(1\)以后,矩阵被分成几个四连通块。
空间限制1M。
思路:
由于空间限制为1M,因此我们需要一个空间\(\mathcal O(n)\)的做法。
考虑并查集,每次遇到相邻的连通块就合并。
由于合并时只需要考虑上下两行,此时连通块个数不超过\(2n\),因此我们只需要空间回收,使得并查集上只保留这不超过\(2n\)个结点即可。
源代码:
#include<queue>
#include<cstdio>
#include<cctype>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=3001;
bool inq[N*2];
int a[2][N],bel[2][N],vis[N*2],b[N];
std::queue<int> q;
struct DisjointSet {
int anc[N*2];
void reset(const int &n) {
for(register int i=1;i<=n;i++) {
anc[i]=i;
}
}
int find(const int &x) {
return x==anc[x]?x:anc[x]=find(anc[x]);
}
void merge(const int &x,const int &y) {
anc[find(x)]=find(y);
}
bool same(const int &x,const int &y) {
return find(x)==find(y);
}
};
DisjointSet s;
int main() {
const int n=getint();
s.reset(n*2);
for(register int i=1;i<=n*2;i++) {
inq[i]=true;
q.push(i);
}
int ans=0;
for(register int i=1;i<=n;i++) {
const int cur=i&1;
for(register int j=1;j<=n;j++) {
a[cur][j]=getint();
if(a[cur][j]) continue;
bel[cur][j]=q.front();
inq[q.front()]=false;
q.pop();
ans++;
if(i!=1&&!a[!cur][j]) {
const int x=bel[cur][j],y=bel[!cur][j];
if(!s.same(x,y)) {
s.merge(x,y);
ans--;
}
}
if(j!=1&&!a[cur][j-1]) {
const int x=bel[cur][j],y=bel[cur][j-1];
if(!s.same(x,y)) {
s.merge(x,y);
ans--;
}
}
}
for(register int j=1;j<=n;j++) {
if(!a[cur][j]) {
vis[s.find(bel[cur][j])]=i;
b[j]=s.find(bel[cur][j]);
}
}
for(register int j=1;j<=n*2;j++) {
if(vis[j]!=i) {
if(inq[j]) continue;
q.push(j);
inq[j]=true;
s.anc[j]=j;
}
}
for(register int j=1;j<=n;j++) {
if(!a[cur][j]) bel[cur][j]=b[j];
}
}
printf("%d\n",ans);
return 0;
}
[ZOJ1482]Partitions的更多相关文章
- Partitioning & Archiving tables in SQL Server (Part 2: Split, Merge and Switch partitions)
Reference: http://blogs.msdn.com/b/felixmar/archive/2011/08/29/partitioning-amp-archiving-tables-in- ...
- Rotate partitions in DB2 on z
Rotating partitions You can use the ALTER TABLE statement to rotate any logical partition to becom ...
- How to choose the number of topics/partitions in a Kafka cluster?
This is a common question asked by many Kafka users. The goal of this post is to explain a few impor ...
- rabbitmq之partitions
集群为了保证数据一致性,在同步数据的同时也会通过节点之间的心跳通信来保证对方存活.那如果集群节点通信异常会发生什么,系统如何保障正常提供服务,使用何种策略回复呢? rabbitmq提供的处理脑裂的方法 ...
- ADF_Database Develop系列2_设计数据库表之Table Partitions/Create Users/Generate DDL
2013-05-01 Created By BaoXinjian
- PostgreSQL Partitions
why we need partitions The first and most demanding reason to use partitions in a database is to inc ...
- mypc--------------->lspci,lsusb,meminfo cpuinfo ioports filesystems interrupts mounts net partitions pagetypeinfo slabinfo timer_list uptime version zoneinfo 等配置信息
[user@username home]$ lspci00:00.0 Host bridge: Intel Corporation 4th Gen Core Processor DRAM Contro ...
- Oracle DB 分区特性概述 Overview of Partitions
概述:在Oracle数据库中,分区(partitioning)可以使非常大的表(table)或索引(index)分解为小的易管理的块(pieces),这些块被称作分区(partitions).每个分区 ...
- Project Euler 78:Coin partitions
Coin partitions Let p(n) represent the number of different ways in which n coins can be separated in ...
随机推荐
- jenkins 实现测试发布、预发布、真实发布、回滚发布
主要思路: 1.做三个文件夹,用于放置不可随意修改的配置文件(测试发布.预发布.真实发布) 2.每次都先修改配置文件再进行构建(构建时会先把配置文件复制到构建的目录,再同步到发布的目录) 3.发布完代 ...
- 论文阅读笔记三十三:Feature Pyramid Networks for Object Detection(FPN CVPR 2017)
论文源址:https://arxiv.org/abs/1612.03144 代码:https://github.com/jwyang/fpn.pytorch 摘要 特征金字塔是用于不同尺寸目标检测中的 ...
- Ajax 异步局部刷新
Ajax 异步局部刷新 AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着可以在不重新加载整个网页 ...
- cPanel中添加设置附加域(Addon domain)
本文介绍cPanel设置附加域(addon domain)来实现一个空间做多个网站的方法. 附加域(addon domain) 作用:通过它可以实现添加 新的顶级域名绑定到主机,从而创建新的站点.例如 ...
- 用Photoshop扭曲滤镜工具打造旋转的光束效果
原文:https://www.w3cschool.cn/photoshopgjjc/dthfn1.html 教程与已往的不同,作者加上了一些新的步骤如锐化等,出来的效果也有所改进.大致过程:先用滤镜制 ...
- (三)apache的安装与配置
一.安装: 推荐使用cygwin自带的Setup.exe.带来的好处不言而喻,所有安装的程序都是经过测试的,这样确保你不会把宝贵的时间浪费来毫无意义的劳动上. 在安装程序中选择两个包就行了(分别是ap ...
- 笔记本电脑清除BIOS密码
进bios前输错3次密码弹出八位溢出码为A 用下面的网站计算出结果为B BIOS Master Password Generator for Laptops 登陆bios,输错3次密码后输入B,就可以 ...
- Codeforces 986D Perfect Encoding FFT 分治 高精度
原文链接https://www.cnblogs.com/zhouzhendong/p/9161557.html 题目传送门 - Codeforces 986D 题意 给定一个数 $n(n\leq 10 ...
- POJ1459 Power Network 网络流 最大流
原文链接http://www.cnblogs.com/zhouzhendong/p/8326021.html 题目传送门 - POJ1459 题意概括 多组数据. 对于每一组数据,首先一个数n,表示有 ...
- 用vs调式docker环境下web程序的一处坑
新建web项目,此时launchSettings.json文件为: "iisSettings": { "windowsAuthentication": fals ...