Counting Islands II

描述

Country H is going to carry out a huge artificial islands project. The project region is divided into a 1000x1000 grid. The whole project will last for N weeks. Each week one unit area of sea will be filled with land.

As a result, new islands (an island consists of all connected land in 4 -- up, down, left and right -- directions) emerges in this region. Suppose the coordinates of the filled units are (0, 0), (1, 1), (1, 0). Then after the first week there is one island:

#...
....
....
....

After the second week there are two islands:

#...
.#..
....
....

After the three week the two previous islands are connected by the newly filled land and thus merge into one bigger island:

#...
##..
....
....

Your task is track the number of islands after each week's land filling.

输入

The first line contains an integer N denoting the number of weeks. (1 ≤ N ≤ 100000)

Each of the following N lines contains two integer x and y denoting the coordinates of the filled area.  (0 ≤ x, y < 1000)

输出

For each week output the number of islands after that week's land filling.

样例输入
3
0 0
1 1
1 0
样例输出
1
2
1
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e5+;
using namespace std;
using namespace __gnu_cxx;
const int N = ;
int pre[N],ans[][];
int d[][]= {,,,,,-,-,};
int n,cnt=;
set<int>sa;
int Find(int x)
{
if(pre[x] != x)
pre[x] = Find(pre[x]);
return pre[x];
}
void init()
{
for(int i=; i<N; i++)pre[i]=i;
}
int main()
{
init();
int X,Y;
int g=;
cin>>n;
for(int i=; i<n; i++)
{
g++;
cin>>X>>Y;
ans[X][Y]=;
for(int j=; j<; j++)
{
int xx=X+d[j][];
int yy=Y+d[j][];
if(xx>=&&xx<&&yy>=&&yy<)
{
if(ans[xx][yy]==)
{
int aa=Find(xx*+yy),bb=Find(X*+Y);
if(aa!=bb)pre[aa]=bb,g--;
}
}
}
cout<<g<<endl;
}
return ;
}

hihocoder Counting Islands II(并查集)的更多相关文章

  1. Counting Islands II

    Counting Islands II 描述 Country H is going to carry out a huge artificial islands project. The projec ...

  2. Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环

    D. Dividing Kingdom II   Long time ago, there was a great kingdom and it was being ruled by The Grea ...

  3. [hihoCoder]无间道之并查集

    题目大意: #1066 : 无间道之并查集 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 这天天气晴朗.阳光明媚.鸟语花香,空气中弥漫着春天的气息……额,说远了,总之, ...

  4. hihoCoder 树结构判定(并查集)

    思路:树满足两个条件: 1.顶点数等于边数加一 2.所有的顶点在一个联通块 那么直接dfs或者并查集就可以了. AC代码 #include <stdio.h> #include<st ...

  5. hihocoder 1638:多级并查集

    题目链接 并查集可以用于聚类. import java.io.FileInputStream; import java.io.FileNotFoundException; import java.ut ...

  6. UVA1665 Islands (并查集)

    补题,逆序考虑每个询问的时间,这样每次就变成出现新岛屿,然后用并查集合并统计.fa = -1表示没出现. 以前写过,但是几乎忘了,而且以前写得好丑的,虽然常数比较小,现在重新写练练手.每个单词后面都要 ...

  7. 【占位】HihoCoder 1160 : 攻城略地(并查集好题)

    攻城略地 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 A.B两国间发生战争了,B国要在最短时间内对A国发动攻击.已知A国共有n个城市(城市编号1, 2, …, n),城 ...

  8. HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)

    HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...

  9. HDU 3081 Marriage Match II (二分图,并查集)

    HDU 3081 Marriage Match II (二分图,并查集) Description Presumably, you all have known the question of stab ...

随机推荐

  1. [TJOI2018]数学计算 线段树

    ---题面--- 题解: ,,,考场上看到这题,没想到竟然是省选原题QAQ,考场上把它当数学题想了好久,因为不知道怎么处理有些数没有逆元的问题....知道这是线段树后恍然大悟. 首先可以一开始就建出一 ...

  2. Laravel中Redis的使用

    安装 laravel中使用redis首先需要你通过 Composer 安装 predis/predis 包: composer require predis/predis 配置 redis的配置文件是 ...

  3. pmap用法小计

    By francis_hao    Aug 4,2017   pmap-报告进程的内存映射.   概要 pmap [options] pid [...]   描述 pmap命令用来报告一个或多个进程的 ...

  4. ubuntu下opencv使用cvNamedWindow()和cvShowImage()出错的解决方法

    重装系统和opencv,编译运行显示一副图像的程序,报错如下 liurf@liurf-Lenovo-G470:~/WorkSpace/slambook-master/ch5/imageBasics$ ...

  5. Consumer [分组背包]

    Consumer Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others) Total Subm ...

  6. 编译 openssl 0.9.8zc 出现 error C2220: warning treated as error - no 'object' file generated

    remove the /WX option from CFLAG

  7. AngularJS+BootStrap的一些插件

    插件网址:http://jquerypluginplus.com/ 树  1.angular-bootstrap-nav-tree http://jquerypluginplus.com/angula ...

  8. Spring - IoC(10): 生命周期

    Spring 容器可以管理 singleton 作用域 Bean 的生命周期,容器能够跟踪 Bean 实例的创建.销毁.管理 Bean 生命周期行为主要有两个时机: 注入 Bean 的依赖关系之后 即 ...

  9. 【BZOJ3237】【AHOI2013】连通图 [CDQ分治]

    连通图 Time Limit: 20 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description Input Output Sampl ...

  10. 网络流专题练习Day1

    04/16 一共做了8道题 首先网络流目前自己掌握的只有最大流Dinic算法和普通的费用流算法 有空还要去学习一下SAP和ZKW费用流(flag早早立在前面以后看到都有动力... 但网络流的算法个人认 ...