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个点的连 ...
随机推荐
- HTML5: SVG (可缩放矢量图形)
ylbtech-HTML5: SVG (可缩放矢量图形) 可缩放矢量图形是基于可扩展标记语言(标准通用标记语言的子集),用于描述二维矢量图形的一种图形格式.它由万维网联盟制定,是一个开放标准. 1. ...
- 学习笔记之Unit testing/Integration testing/dotnet test and xUnit
source code https://github.com/haotang923/dotnet/tree/master/src Unit testing C# code in .NET Core u ...
- [转]NuGet 包升级
Update-Package 在 NuGet 的命令控制台执行这个就会升级所有 NuGet 包,不带参数. 使用 VS2015 时,插件 Web Extension Pack 2015 和 Web.E ...
- 防火墙没有关导致外部访问虚拟机的tomcat遇到的问题和解决方法
部署好tomcat,想在自己电脑上的浏览器访问,但是发现访问不了 访问方式是浏览器地址栏输入ip加端口,我的是192.138.211.121:8080,显示结果是无连接 在电脑上ping一下主机发现是 ...
- 基于标准库实现string和wstring的转换
// convert string to wstring std::wstring to_wstring(const std::string& str, const std::locale&a ...
- Ubuntu-14.04.1 desktop安装时及安装后遇到的小问题
ubuntu安装时,进入桌面就黑屏:ctrl+alt+F1进入终端1,输入:startx,此时桌面会重新出现,快速修改桌面的分辨率即可.没有及时修改可以重复上面操作. su root认证失败:sudo ...
- Spark学习笔记1:Spark概览
Spark是一个用来实现快速而通用的集群计算的平台. Spark项目包含多个紧密集成的组件.Spark的核心是一个对由很多计算任务组成的,运行在多个工作机器或者是一个计算集群上的应用进行调度,分发以及 ...
- delphi WebBrowser的使用方法详解(三)
WebBrowser 操作记要 WebBrowser1.GoHome; //到浏览器默认主页 WebBrowser1.Refresh; //刷新 WebBrowser1.GoBack; //后退 ...
- python之路之装饰器
一 装饰器进化之路1) import time def index(): start_time=time.time() time.sleep() print('welcome to index wor ...
- django-mysql表的增删改查
1.增加数据 调用该路由执行ModelsCaozuo的处理方法 第一种实例化类 class ModelsCaozuo(View): ''' 数据库增加数据 ''' def get(self, requ ...