[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 ...
随机推荐
- MongoDB数据库备份与还原、单表的导入导出
-------------------MongoDB备份与恢复------------------- 1.MongoDB数据库备份 1.语法: mongodump -h dbh ...
- 如何用TortoiseSVN对文件进行操作
我们如何用TortoiseSVN修改文件,添加文件,删除文件,以及如何解决冲突等. 添加文件 在检出的工作副本中添加一个Readme.txt文本文件,这时候这个文本文件会显示为没有版本控制的状态,如图 ...
- Android 网络请求框架
1.okHttp 特点 简单.灵活.无连接.无状态 优势: 谷歌官方API在6.0之后在Android SDK中移除了HttpClient,然后他火了起来, 他支持SPDY(谷歌开发的基于TCP应用层 ...
- 微信小程序--WXS---JS 代码插入
主页要使用的JS代码放在index.js里面 例 Page({ data: { array: [1, 2, 3, 4, 5, 1, 2, 3, 4] } }) 只复制以下这段 data: { arra ...
- win10下右键菜单添加“打开cmd”
早期版本的win10是可以在文件夹的左上角打开cmd的,更新后发现现在只有powershell能用了.这不方便. 通过修改注册表,可以实现这个功能. 具体做法:新建一个.reg文件win10_add_ ...
- Html列表分页算法
public class PageHelper { /// <summary> /// 标签 /// </summary> public string Tag { get; s ...
- IDEAL 中配置Tomcat的内存值
-server -XX:PermSize=2048M -XX:MaxPermSize=4096m
- linux服务器性能——CPU、内存、流量、磁盘使用率的监控
https://blog.csdn.net/u012859748/article/details/72731080
- webstorm我用到的快捷键【不断更新】
alt+insert:新建一个文件或其他 ctrl+shift+l:代码格式化 [可能会和qq的锁屏键冲突] ctrl+shift+r:批量查找替换 多点编辑:按住alt键选择多列,就可以编辑多行了 ...
- 令人疑惑的 std::remove 算法
摘自<Effective STL>第32条 remove的声明: template<class ForwardIterator, class T> ForwardIterato ...