[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 ...
随机推荐
- Linux/Unix/Mac OS下的远程访问和文件共享方式
scp -P 20022 src.tar.gz zhouhh@192.168.12.13:/home/zhouhhscp -P 20022 zhouhh@192.168.12.13:/home/zho ...
- HTML5 CSS3 Transform 笔记 (scale不起作用)
Transform的 scale属性不能作用于 inline元素上,例如span 并且动画 animation 也不能作用于inline元素上 可以给span加display:inline-bloc ...
- python杂写
一:用户交互 与用户交互主要使用input,这里需要说明三点: 1:input会等待用户输入 2:会将输入的内容赋值给变量 3:input出的变量都是字符串类型(str) 例子1:注意,因为input ...
- python列表1
List (列表)List(列表) 是 Python 中使用最 频繁的数据类 型.列表 可以 完成大 多数集 合类 的数据 结构 实现. 列表中 元素 的类型 可以 不相同 ,它支 持数 字,字 符串 ...
- JDK 自带压缩解压流
代码如下 package com.test.java.zip; import java.io.BufferedInputStream; import java.io.BufferedOutputStr ...
- Newtonsoft.Json WindowPhone7.1
才发现最新版本的Newtonsoft.Json 已经不支持wp71了… 查了下最后一个支持的版本是Json.NET 5.0 Release 8… 安装的时候需要: Install-Package Ne ...
- The connection string 'MysqlEF' in the application's configuration file does not contain the require异常
在学习EF core first 对接mysql时,出现了这个异常. 原因是:连接字符串中缺少providerName="MySql.Data.MySqlClient" <a ...
- C#操作windows服务
本文主要说明了Windows服务的安装.卸载.启动.停止.获取服务安装路径.获取服务状态.检查服务是否存在.获取服务版本. 我们需要引用命名空间using System.Configuration.I ...
- php7安装参数编译
系统:Centos6.8 软件包:php-7.0.14.tar.gz yum install bzip2 bzip2-devel -y yum install curl curl-devel -y y ...
- ionic2中使用自定义图标
在ionic2中使用自定义图标,如iconfont(阿里巴巴矢量图标). 先在http://www.iconfont.cn/ 中找到自己需要的图标,然后将图标加入购物车,然后下载该图标. 下载完成后解 ...