[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 ...
随机推荐
- python面向对象三大特性之继承
继承是创建新类的方法,以下是几个概念 父类,基类,超类: 被继承的类 子类,派生类:继承而产出的类 单继承:继承一个父类 多继承:继承多个父类 继承: 什么是什么的关系 父类中没有的属性,在字类中出现 ...
- 八卦一下Starlark语言
八卦一下Starlark语言 编译移植TensorFlow时用到Bazel这一构建工具,Bazel用Starlark语法来编写WORKSPACE/BUILD文件,它们是类似于Make中的makeifl ...
- WPF 下两种图片合成或加水印的方式(转载)
来源:http://www.cnblogs.com/lxblog/ 最近项目中应用多次应用了图片合成,为了今后方便特此记下. 在WPF下有两种图片合成的方式,一种还是用原来C#提供的GDI+方式,命名 ...
- 【Oracle】Linux7安装11g 86%报错:Error in invoking target 'agent nmhs' of makefile
http://blog.itpub.net/29475508/viewspace-2120836/
- webpack学习笔记--配置总结
从前面的配置看来选项很多,Webpack 内置了很多功能. 你不必都记住它们,只需要大概明白 Webpack 原理和核心概念去判断选项大致属于哪个大模块下,再去查详细的使用文档. 通常你可用如下经验去 ...
- rabbitmq3.7.5 centos7 集群部署笔记
1. 准备3台 centos服务器 192.168.233.128 192.168.233.130 192.168.233.131 防火墙放开 集群端口, 这里一并把所有rabbitmq ...
- innobackupex做MySQL增量备份及恢复
http://www.cnblogs.com/paul8339/p/6731688.html
- stl测试
以下测试都在学校电脑进行 我觉得应该比考试机器慢一点.. 1.map map的速度测出来和放入数值大小有很大关系 比如 #include <bits/stdc++.h> using nam ...
- .net core2.1 - ef core数据库迁移,初始化种子数据
起因:早上偶然看见一篇文章说是ef core(2.x)使用种子数据,主表子表迁移时候,正常情况下说是无法迁移成功,索性就试试,结果是和ef6的一样,没感觉有什么大的区别.一切OK,见下面内容. 1.首 ...
- mysql基本操作(二)
1.向表msg中插入数据,先创建表,再插入数据 mysql> create table msg ( -> id int, -> title varchar(60), -> na ...