UVA1665 Islands (并查集)
补题,逆序考虑每个询问的时间,这样每次就变成出现新岛屿,然后用并查集合并统计。fa = -1表示没出现。
以前写过,但是几乎忘了,而且以前写得好丑的,虽然常数比较小,现在重新写练练手。每个单词后面都要加空格不然PE
#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
const int maxq = 1e5+; int q[maxq];
int pa[maxn*maxn]; int Find(int x){ return x==pa[x]?x:pa[x]=Find(pa[x]); } int h[maxn][maxn]; struct Node
{
int x,y,val;
}nd[maxn*maxn]; bool operator < (const Node &x,const Node &y) { return x.val < y.val; } int dx[] = {,,-,};
int dy[] = {-,,,}; int main()
{
//freopen("in.txt","r",stdin);
int Z; scanf("%d",&Z);
while(Z--){
int m,n; scanf("%d%d",&m,&n);
for(int i = ; i < m; i++){
for(int j = ; j < n; j++){
scanf("%d",h[i]+j);
int t = i*n+j;
nd[t].x = i; nd[t].y = j;
nd[t].val = h[i][j];
}
} sort(nd,nd+m*n);
memset(pa,-,sizeof(int)*(m*n));
int Ts; scanf("%d",&Ts);
for(int i = ; i < Ts; i++){
scanf("%d",q+i);
} int k = m*n-;
int ans = ;
for(int i = Ts-; i >= ; i--){
if(q[i]<nd[k].val){
while(k>=&&q[i]<nd[k].val){
int id = nd[k].x*n+nd[k].y;
if(!~pa[id]) ans++,pa[id] = id;
for(int d = ; d < ; d++){
int nx = nd[k].x+dx[d], ny = nd[k].y+dy[d];
if(nx>=&&nx<m&&ny>=&&ny<n&&h[nx][ny]>q[i]){
int nid = nx*n+ny;
if(~pa[nid]) {
int a = Find(nid), b = Find(id);
if(a != b) {
pa[a] = b;
ans--;
}
}
}
}
k--;
}
if(k<){
for(;i>=;i--){
q[i] = ans;
}
break;
}
}
q[i] = ans;
}
for(int i = ; i < Ts; i++) printf("%d ",q[i]);
putchar('\n');
}
return ;
}
UVA1665 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 ...
- cdoj203-Islands 【并查集】
http://acm.uestc.edu.cn/#/problem/show/203 Islands Time Limit: 30000/10000MS (Java/Others) Memor ...
- 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个点的连 ...
随机推荐
- matlab白底换红底
白底换红底 局部阈值调整 边界高斯滤波 function demo global R global threshold global img global dstColor global rect g ...
- python使用xlrd操作Excel文件
一.xlrd读取Excel文件 用xlrd进行读取比较方便,流程和平常手动操作Excel一样,打开工作簿(Workbook),选择工作表(sheets),然后操作单元格(cell). 例子:要打开当前 ...
- Python使用Timer实现验证码功能
from threading import Timer import random class Code: def __init__(self): self.make_cache() def make ...
- 20个Flutter实例视频教程-第12节: 流式布局 模拟添加照片效果
视频地址: https://www.bilibili.com/video/av39709290/?p=12 博客地址: https://jspang.com/post/flutterDemo.html ...
- Identity Server 4 原理和实战(完结)_----选看 OAuth 2.0 简介(上)
https://www.yuque.com/yuejiangliu/dotnet/cg95ni 代表资源所有者的凭据 授权 Authorization Grant 授权是一个代表着资源所有者权限的凭据 ...
- flex+blazeds
BlazeDS开发指南: http://www.cnblogs.com/xia520pi/archive/2012/05/26/2519343.html 使用BlazeDS实现Flex和Java通信 ...
- HDU - 3410 Passing the Message 单调递减栈
Passing the Message What a sunny day! Let’s go picnic and have barbecue! Today, all kids in “Sun Flo ...
- OpenCV第一课
1.OpenCV下载地址:http://opencv.org/downloads.html 因为本人电脑装的是vs2010,所以下载的是opencv-2.4.11.exe(vc10.vc11.vc12 ...
- scrapy.Request使用meta传递数据,以及deepcopy的使用
scrapy.Request(url[,callback,method="GET",headers,body,cookies,meta,dont_filter=False]) ...
- c++中初始化列表的初始化变量顺序问题
例题来看:请问下面程序打印出的结果是什么? #include <iostream> #include <string> using namespace std; class b ...