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 ...
随机推荐
- [USACO08MAR]土地征用Land Acquisition
题面在这里 题意 约翰准备扩大他的农场,眼前他正在考虑购买N块长方形的土地. 如果约翰单买一块土地,价格就是土地的面积,但他可以选择并购一组土地, 并购的价格为这些土地中最大的长乘以最大的宽. 给定每 ...
- vector 搜索
http://classfoo.com/ccby/article/cIBahI #include <iostream> #include <algorithm> #includ ...
- MySQL使用笔记(六)条件数据记录查询
By francis_hao Dec 17,2016 条件数据记录查询 mysql> select field1,field2-- from table_name where 条件; 其中 ...
- git使用笔记(四)远程操作
By francis_hao Nov 19,2016 以一张图说明远程操作,图片来自参考[2] git clone 从远端主机克隆一个版本库,若省略directory则生成一个和远端同名的版本库 ...
- POJ2912:Rochambeau(带权并查集)
Rochambeau Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5208 Accepted: 1778 题目链接:h ...
- rest与restful
知乎上面摘抄的,感觉不错,分享下: https://www.zhihu.com/question/28557115 1. REST描述的是在网络中client和server的一种交互形式:RES ...
- Spring学习--依赖注入的方式
Spring 依赖注入: 属性注入(最常使用) 构造函数注入 工厂方法注入(很少使用,不推荐) 属性注入:通过 setter 方法注入 Bean 的属性值或依赖的对象 , 使用<property ...
- jquery中的get和post、ajax有关返回值的问题描述
一:前言 今天我就要离开公司回学校准备考试,在走之前,我自己做的一个模块测试除了一些小的bug.问题如下 我在往数据库中插入数据,首先要选择一级菜单,接着会更具一级菜单生成一级菜单的子目录,在选择日期 ...
- Eclipse+Tomcat实现热部署/热加载配置,修改java代码无需重启tomcat
一.Tomcat热加载配置 Eclipse Package Explorer中找到Servers,点击你所需要运行的tomcat的config配置文件,例如 demo-config,双击该文件夹下的s ...
- NYOJ 973 天下第一 (最短路)
题目链接 描述 AC_Grazy一直对江湖羡慕不已,向往着大碗吃肉大碗喝酒的豪情,但是"人在江湖漂,怎能 不挨刀","人在江湖身不由己",如果自己的武功太差,在 ...