hihocoder Counting Islands II(并查集)
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(并查集)的更多相关文章
- Counting Islands II
Counting Islands II 描述 Country H is going to carry out a huge artificial islands project. The projec ...
- 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 ...
- [hihoCoder]无间道之并查集
题目大意: #1066 : 无间道之并查集 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 这天天气晴朗.阳光明媚.鸟语花香,空气中弥漫着春天的气息……额,说远了,总之, ...
- hihoCoder 树结构判定(并查集)
思路:树满足两个条件: 1.顶点数等于边数加一 2.所有的顶点在一个联通块 那么直接dfs或者并查集就可以了. AC代码 #include <stdio.h> #include<st ...
- hihocoder 1638:多级并查集
题目链接 并查集可以用于聚类. import java.io.FileInputStream; import java.io.FileNotFoundException; import java.ut ...
- UVA1665 Islands (并查集)
补题,逆序考虑每个询问的时间,这样每次就变成出现新岛屿,然后用并查集合并统计.fa = -1表示没出现. 以前写过,但是几乎忘了,而且以前写得好丑的,虽然常数比较小,现在重新写练练手.每个单词后面都要 ...
- 【占位】HihoCoder 1160 : 攻城略地(并查集好题)
攻城略地 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 A.B两国间发生战争了,B国要在最短时间内对A国发动攻击.已知A国共有n个城市(城市编号1, 2, …, n),城 ...
- HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)
HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...
- HDU 3081 Marriage Match II (二分图,并查集)
HDU 3081 Marriage Match II (二分图,并查集) Description Presumably, you all have known the question of stab ...
随机推荐
- [Leetcode] palindrome partition ii 回文分区
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 【NOIP模拟赛】公主的朋友 区间染色问题
这道题大家都用的分块,然而我发现这是一个经典算法:区间染色问题. 我们区间染色时把区间分成若干连续的颜色段,然后我们每次染色删除原来的颜色段插入新的颜色段. 我们发现我们的时间复杂度直接与我们要染色区 ...
- 十个迅速提升JQuery性能的技巧
本文提供即刻提升你的脚本性能的十个步骤.不用担心,这并不是什么高深的技巧.人人皆可运用!这些技巧包括: 使用最新版本 合并.最小化脚本 用for替代each 用ID替代class选择器 给选择器指定前 ...
- Win7命令mklink的使用
C盘空间越来越小,在Win7里还标红了,心里看得不舒服,得想一些方法腾出一些空间.看了AppData,Chrome占了1G多的空间. 当时安装Chrome浏览器时因为不能指定安装目录,所以Chrome ...
- POJ1308:Is It A Tree?(并查集)
Is It A Tree? 题目链接:http://poj.org/problem?id=1308 Description: A tree is a well-known data structure ...
- Eclipse来push,fetch,rebase代码
如何与项目里的其他人一起合作项目,提交代码并更新呢?这里提出我比最近用到的两种工具:一种是Eclipse,另外一个是SourceTree.个人推荐从事Java开发的话,可以用Eclipse.当然,还有 ...
- Spring学习-- SpEL表达式
Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言. 语法类似于 EL:SpEL 使用 #{...} 作为定界符 , 所有在大括号中的字符都将被认为是 SpE ...
- es6+最佳入门实践(13)
13.模块化 13.1.什么是模块化 模块化是一种处理复杂系统分解为更好的可管理模块的方式.通俗的讲就是把一个复杂的功能拆分成多个小功能,并且以一种良好的机制管理起来,这样就可以认为是模块化.就像作家 ...
- python imageai 对象检测、对象识别
imageai库里面提供了目标识别,其实也可以说是目标检测,和现在很多的收集一样就是物体识别.他可以帮你识别出各种各样生活中遇见的事物.比如猫.狗.车.马.人.电脑.收集等等. 感觉imageai有点 ...
- nginx启动脚本(class练习)
说明:使用类的方式编写程序启动脚本(练习) 1 #!/usr/bin/env python import sys import os from subprocess import Popen,PIPE ...