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的最大价值; 这里 ...
随机推荐
- VUE组件如何与iframe通信问题
vue组件内嵌一个iframe,现在想要在iframe内获取父vue组件内信息,由于本人技术有限,采用的是H5新特性PostMessage来解决跨域问题. postMessage内涵两个API: on ...
- bzoj3109【CQOI2013】新数独
3109: [cqoi2013]新数独 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 365 Solved: 229 [Submit][Statu ...
- PL/SQL Developer导入导出Oracle数据库方法
前一篇博客介绍了Navicat工具备份Oracle的方法.这篇博客介绍一下使用PL/SQL Developer工具导入导出Oracle数据库的方法. PL/SQL Developer是Oracle数据 ...
- Codeforces 794F. Leha and security system 线段树
F. Leha and security system Bankopolis, the city you already know, finally got a new bank opened! ...
- Flame Graphs
http://www.brendangregg.com/flamegraphs.html Flame graphs are a visualization of profiled software, ...
- WebService注解汇总
Web Service 元数据注释(JSR 181) @WebService 1.serviceName: 对外发布的服务名,指定 Web Service 的服务名称:wsdl:service.缺省值 ...
- Node.js 101(2): Promise and async
--原文地址:http://blog.chrisyip.im/nodejs-101-package-promise-and-async 先回想一下 Sagase 的项目结构: lib/ cli.js ...
- PR 修改保存的增强 ME_UPDATE_REQUISITION
FUNCTION me_update_requisition."""""""""""&qu ...
- YTU 1010: 目标柏林
1010: 目标柏林 时间限制: 1000 Sec 内存限制: 64 MB 提交: 32 解决: 15 题目描述 1945年初,苏军和英美联军已从东西两面攻入德国国境. 4月初,在苏军和英美联军的 ...
- YTU 2946: 填空:间接基类就是A
2946: 填空:间接基类就是A 时间限制: 1 Sec 内存限制: 128 MB 提交: 132 解决: 96 题目描述 如下程序所示,D继承自B和C,而B和C均继承自A.根据继承的机制,D的对 ...