hihocoder offer收割编程练习赛11 C 岛屿3
思路:
并查集的应用。
实现:
#include <iostream>
#include <cstdio>
using namespace std; bool a[][];
int n, x, y;
int par[];
int ran[];
int dx[] = { , , -, };
int dy[] = { , , , - }; void init(int n)
{
for (int i = ; i < n; i++)
{
ran[i] = ;
par[i] = i;
}
} int find(int x)
{
if (par[x] == x)
return x;
return par[x] = find(par[x]);
} void unite(int x, int y)
{
x = find(x);
y = find(y);
if (x == y)
return;
if (ran[x] < ran[y])
par[x] = y;
else
{
par[y] = x;
if (ran[x] == ran[y])
ran[x]++;
}
} bool same(int x, int y)
{
return find(x) == find(y);
} int trans(int x, int y)
{
return x * + y;
} int main()
{
init();
cin >> n;
int now = , c = ;
for (int i = ; i < n; i++)
{
now++;
c += ;
cin >> x >> y;
a[x][y] = true;
int tmp = trans(x, y);
for (int j = ; j < ; j++)
{
int nx = x + dx[j];
int ny = y + dy[j];
if (nx >= && nx < && ny >= && ny < && a[nx][ny])
{
int t = trans(nx, ny);
if (!same(tmp, t))
{
unite(tmp, t);
now--;
}
c -= ;
}
}
cout << now << " " << i + << " " << c << endl;
}
return ;
}
hihocoder offer收割编程练习赛11 C 岛屿3的更多相关文章
- hihocoder offer收割编程练习赛11 D 排队接水
思路: 莫队算法+树状数组. 莫队算法的基本思想是对大量要查询的区间进行离线处理,按照一定的顺序计算,来降低复杂度.概括来说,我们在知道了[l, r]的解,并且可以通过一个较低的复杂度推出[l - 1 ...
- hihocoder offer收割编程练习赛11 B 物品价值
思路: 状态压缩 + dp. 实现: #include <iostream> #include <cstdio> #include <cstring> #inclu ...
- hihocoder offer收割编程练习赛11 A hiho字符串
思路: 我用的尺取. 注意题目描述为恰好2个'h',1个'i',1个'o'. 实现: #include <iostream> #include <cstdio> #includ ...
- hihocoder [Offer收割]编程练习赛4
描述 最近天气炎热,小Ho天天宅在家里叫外卖.他常吃的一家餐馆一共有N道菜品,价格分别是A1, A2, ... AN元.并且如果消费总计满X元,还能享受优惠.小Ho是一个不薅羊毛不舒服斯基的人,他希望 ...
- hihocoder [Offer收割]编程练习赛61
[Offer收割]编程练习赛61 A:最小排列 给定一个长度为m的序列b[1..m],再给定一个n,求一个字典序最小的1~n的排列A,使得b是A的子序列. 贪心即可,b是A的子序列,把不在b中的元素, ...
- 【[Offer收割]编程练习赛11 C】岛屿3
[题目链接]:http://hihocoder.com/problemset/problem/1487 [题意] 中文题 [题解] 岛屿的数目对应了这个图中联通块的数目; 面积则对应有多少个方块; 周 ...
- ACM学习历程—Hihocoder [Offer收割]编程练习赛1
比赛链接:http://hihocoder.com/contest/hihointerview3/problem/1 大概有一个月没怎么打算法了.这一场的前一场BC,也打的不是很好.本来Div1的A和 ...
- hihocoder offer收割编程练习赛8 C 数组分拆
思路:(引自bfsoyc的回答:http://hihocoder.com/discuss/question/4160) 动态规划.状态dp[i]表示 前i个数的合法的方案数,转移是 dp[i] = s ...
- 【[Offer收割]编程练习赛11 B】物品价值
[题目链接]:http://hihocoder.com/problemset/problem/1486 [题意] [题解] 设f[i][j]表示前i个物品,每种属性的状态奇偶状态为j的最大价值; 这里 ...
随机推荐
- Windows下的Jupyter Notebook 的介绍(写给新手)(图文详解)
不多说,直接上干货! Windows下的Python 3.6.1的下载与安装(适合32bits和64bits)(图文详解) Windows下的Jupyter Notebook 安装与自定义启动(图文详 ...
- 解决javah生成.h头文件找不到找不到android.support.v7.app.AppCompatActivity的问题
问题描写叙述: 在使用Android Studio进行JNI开发时,须要使用javah生成C或C++的头文件,可是可能会遇到: 错误: 无法訪问android.support.v7.app.AppCo ...
- 2016/3/10 PHP (超文本预处理器) 是什么?
PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言.语法吸收了C语言.Java和Perl的特点,利于学习,使用广泛,主要适用于W ...
- 3.myeclipse 8.5 m1 注册码
为了能在eclipse 中方便的使用uml,尝试了多次安装各种uml插件未果,myeclipse 自带uml插件,但是要注册啊,要破解啊!!! user:baiduzhidaopassword:oLR ...
- debian包之间的关系
1 debian包之间存在两大类关系 第一,依赖 第二,冲突 2 依赖类关系 2.1 depends 2.2 pre-depends 2.3 recommends 2.4 suggests 2.5 e ...
- Using Python with TurboGears A complete web framework integrating several Python projects
Using Python with TurboGears TurboGears is a Python web framework based on the ObjectDispatch paradi ...
- Android vector Path Data画图详解
SVG是一种矢量图格式,是Scalable Vector Graphics三个单词的首字母缩写.在xml文件中的标签是,画出的图形可以像一般的图片资源使用,例子如下: <vector xmlns ...
- PHPExcel使用收藏
注意:PHP7版本中phpexcel导出文件是提示找不到文件,需修改PHPExcel目录下的calculation目录下的Functions.php的581行 去掉break; 下面是总结的几个使用 ...
- SELinux 初探
SELinux:Security Enhanced Linux.SELinux 是 NSA(美国国家安全局)开发设计,整合到 Linux 内核中的一个模块. 0. 基本概念 DAC(Discretio ...
- 如何编写linux下nand flash驱动-1
1. 硬件特性: [Flash的硬件实现机制] Flash全名叫做Flash Memory,属于非易失性存储设备(Non-volatile Memory Device),与此相对应的是易失 ...