[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 ...
随机推荐
- HTMLTestRunner 美化版本
前言 最近小伙伴们在学玩python,,看着那HTMLTestRunner生成的测试报告,左右看不顺眼,终觉得太丑.搜索了一圈没有找到合适的美化报告,于是忍不住自已动手进行了修改,因习惯python ...
- AI-restful接口写法
AI-restful接口写法 restful接口规范 http协议请求方式:GET POST DELETE PUT PATCH OPTION HEAD 设计接口时必须使用这种格式的数据 GET 查看数 ...
- 数据库和linux对大小写的区分
- # 20165206 2017-2018-2 《Java程序设计》第4周学习总结
20165206 2017-2018-2 <Java程序设计>第4周学习总结 教材学习内容总结 继承:继承是一种由已有的类创建新类的机制. 子类和父类:由继承得到的类称为子类,被继承的类称 ...
- 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)
一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件. 在Spring Cloud中,有分布式配置中心组件spring cloud confi ...
- C# 5.0 CallerMemberName CallerFilePath CallerLineNumber获取调用方法名称,路径,行号
class Program { static void Main(string[] args) { Log("测试"); Console.Read(); } public stat ...
- javascript OOP实例—探测器
<script> /*所有探测器都有探测的方法和分析的方法,分析当前的浏览器环境,不管是浏览器还是nodejs*/ /*container容器探测器*/ /*link链接探测器*/ /*外 ...
- JSP基础知识➣获取参数和过滤器(四)
JSP表单提交和参数获取 JSP表单提交的两种方式:post和get,通过这两种方式提交的参数到后台,获取参数的值主要由request来处理,获取值的方式有以下几种: getParameter(): ...
- Get与Post区别小结
Get:是以实体的方式得到由请求Url所指定资源的信息,如果请求Url只是一个数据产生过程,那么最终要在实体中返回的是处理过程的结果所指向的资源,而不是处理过程的描述. Post:是用来向 ...
- JMeter中BeanShell的实际应用
使用Jmeter的BeanShell断言,把响应数据中的JSON跟数据库中的记录对比 很多时候我们需要把Response Data取到的 Json 字符串跟数据库里的对比,来验证接口的正确性,使用Be ...