cdoj203-Islands 【并查集】
http://acm.uestc.edu.cn/#/problem/show/203
Islands
Time Limit: 30000/10000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
Deep in the Carribean, there is an island even stranger than the Monkey Island, dwelled by Horatio Torquemada Marley. Not only it has a rectangular shape, but is also divided into an n×m grid. Each grid field has a certain height. Unfortunately, the sea level started to raise and in year i, the level is i meters. Another strange feature of the island is that it is made of sponge, and the water can freely flow through it. Thus, a grid field whose height is at most the current sea level is considered flooded.Adjacent unflooded fields (i.e., sharing common edge) create unflooded areas. Sailors are interested in the number of unflooded areas in a given year.
An example of a 4×5 island is given below. Numbers denote the heights of respective fields in meters.Unflooded fields are darker; there are two unflooded areas in the first year and three areas in the second year.
Input
Multiple Test Cases
The input contains several test cases. The first line of the input contains a positive integer Z≤20,denoting the number of test cases. Then Z test cases follow, each conforming to the format described in section Single Instance Input. For each test case, your program has to write an output conforming to the format described in section Single Instance Output.
Single Instance Input
The first line contains two numbers n and m separated by a single space, the dimensions of the island, where 1≤n,m≤1000. Next n lines contain m integers from the range [1,109] separated by single spaces, denoting the heights of the respective fields. Next line contains an integer T (1≤T≤105). The last line contains T integers tj , separated by single spaces, such that 0≤t1≤t2≤⋯≤tT≤109
Output
Single Instance Output
Your program should output a single line consisting of T numbers rj , where rj is the number of unflooded areas in year tj . After every number ,you must output a single space!
Sample input and output
Sample Input | Sample Output |
---|---|
1 |
2 3 1 0 0 |
思路:
这种联通块的问题一看就知道是并查集的思想。
做法:从高水位到低水位依序进行操作,这样每次都有新的块浮出水面,可以在前面的基础上进行合并集合的操作。
给每个位置分配一个数字,方便合并集合。同时将这些数字也排一个序,降低枚举的复杂度。合并集合时向四周查询浮出水面但是没有合并到同一集合的点进行合并。
代码:
#include <fstream>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib> using namespace std; #define PI acos(-1.0)
#define EPS 1e-20
#define lll __int64
#define ll long long
#define INF 0x7fffffff
#define INT 2147483646
const int N=; struct node{
int x,y,h;
}a[N*N]; int n,m,t;
int tt[N*],fa[N*N],cnt[N*];
int xy[][]={{,,,-},{,-,,}}; bool cmp(const node &r1,const node &r2);
inline bool Check(int x,int y);
inline int Find(int x); int main(){
//freopen("D:\\input.in","r",stdin);
//freopen("D:\\output.out","w",stdout);
int T;
scanf("%d",&T);
while(T--){
int tn=;
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
for(int j=;j<m;j++){
scanf("%d",&a[tn].h);
a[tn].x=i;
a[tn++].y=j;
}
}
sort(a,a+n*m,cmp);
scanf("%d",&t);
for(int i=;i<t;i++){
scanf("%d",&tt[i]);
}
memset(fa,-,sizeof(fa));
memset(cnt,,sizeof(cnt));
for(int i=t-,j=;i>=;i--){
cnt[i]=cnt[i+];
while(){
if(j>=tn) break;
int tm=a[j].x*m+a[j].y;
if(a[j].h<=tt[i]||fa[tm]!=-) break;
cnt[i]++,fa[tm]=tm;
for(int k=;k<;k++){
int tx=a[j].x+xy[][k];
int ty=a[j].y+xy[][k];
if(Check(tx,ty)){
int it=tx*m+ty;
if(fa[it]!=-&&Find(it)!=fa[tm]) fa[fa[it]]=fa[tm],cnt[i]--;
}
}
j++;
}
}
for(int i=;i<t;i++)
printf("%d ",cnt[i]);
puts("");
}
return ;
}
bool cmp(const node &r1,const node &r2){
return r1.h>r2.h;
}
inline bool Check(int x,int y){
return x>=&&x<n&&y>=&&y<m;
}
inline int Find(int x){
return fa[x]==x?x:fa[x]=Find(fa[x]);
}
cdoj203-Islands 【并查集】的更多相关文章
- hust 1385 islands 并查集+搜索
islands Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/problem/show/1385 Descri ...
- USACO环绕岛屿Surround the Islands 并查集 枚举暴力
题目描述 Farmer John has bought property in the Caribbean and is going to try to raise dairy cows on a b ...
- CDOJ 203 并查集+优先队列 好题
题目链接 Islands Time Limit: 30000/10000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) S ...
- 【LeetCode】并查集 union-find(共16题)
链接:https://leetcode.com/tag/union-find/ [128]Longest Consecutive Sequence (2018年11月22日,开始解决hard题) 给 ...
- luoguP3224 [HNOI2012]永无乡【线段树,并查集】
洞庭青草,近中秋,更无一点风色.玉鉴琼田三万顷,着我扁舟一叶.素月分辉,明河共影,表里俱澄澈.悠然心会,妙处难与君说. 应念岭表经年,孤光自照,肝胆皆冰雪.短发萧骚襟袖冷,稳泛沧溟空阔.尽挹西江,细斟 ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- 关押罪犯 and 食物链(并查集)
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- bzoj1854--并查集
这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...
随机推荐
- Mysql-binlog的移动和归档
#!/bin/bash # To backup and archive binlogs. declare -i NUM=0 declare -i SUM=0 SUM=`/bin/ls -l mysql ...
- Hessian与Webservice的区别
Hessian:hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能,相比WebService,Hessian更简单.快捷. 采用的是二进制RPC协议,因为 ...
- [ffmpeg]deocde audio(v3.3.2)
/* * Copyright (c) 2001 Fabrice Bellard * * Permission is hereby granted, free of charge, to any per ...
- hive-hbase-handler方式导入hive表数据到hbase表中
Hive与HBase的整合功能的实现是利用两者本身对外的API接口互相进行通信,相互通信主要是依靠hive-hbase-handler.jar工具类 : hive-hbase-handler.jar在 ...
- html5 如何实现客户端验证上传文件的大小
在HTML 5中,现在可以在客户端进行文件上传时的校验了,比如用户选择文件后,可以 马上校验文件的大小和属性等.本文章向码农介绍html5 如何实现客户端验证上传文件的大小,感兴趣的码农可以参考一下. ...
- css (具体代码看笔记本)
参考:https://www.cnblogs.com/liwenzhou/p/7999532.html 1. CSS语法 选择器 {属性1:值1;...;} 2. CSS导入方式 1. 行 ...
- MySQL安装、基本账户安全(5.0以后版本)
博文目录: 1.Mysql-5.0.40.tar.gz Mysql-5.1.72.tar.gz 2.Mysql-5.5.22.tar.gz 3.Mysql-5.5.34.tar.gz 4.Mysql- ...
- HTML5 位运算符
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 我对if(!this.IsPostBack)的理解
if(!this.IsPostBack) { } 通常用在page_load中,获取一个值,该值指示该页是否正为响应客户端回发而加载,或者它是否正被首次加载和访问,如果是为响应客户端回发而加载该页,则 ...
- Java System.getProperty("java.io.tmpdir") 获取系统临时目录
System.getProperty("java.io.tmpdir") 是获取操作系统的缓存临时目录 在windows7中的目录是: C:\Users\登录用户~1\AppDat ...