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的最大价值; 这里 ...
随机推荐
- HDU OJ u Calculate e
这是一道简单的数学计算问题 主义好输出格式就好 #include<stdio.h> int main() { printf("n e\n- -----------\n&quo ...
- hdu 4970 Killing Monsters(数组的巧妙运用) 2014多校训练第9场
pid=4970">Killing Monsters ...
- SQL Server中一些有用的日期sql语句
SQL Server中一些有用的日期sql语句 1.一个月第一天的 SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) 2.本周的星期一 SELECT DA ...
- Codeforces Little Dima and Equation 数学题解
B. Little Dima and Equation time limit per test 1 second memory limit per test 256 megabytes input s ...
- 注入式开发(二):.NET 匿名函数
其实匿名函数就是个委托.只不过写起来更简洁. 为啥要用匿名函数呢?只是为了装逼吗? 诺诺诺 比如说,我们写代码,写着写着,发现有2个函数非常相像: string methodA(string data ...
- iOS开发——高级篇——FMDB 数据库简单使用
#import <Foundation/Foundation.h> @interface UserDB : NSObject // 把userDB设计成一个单例类 + (id)shareI ...
- Why was 80 Chosen as the Default HTTP Port and 443 as the Default HTTPS Port?
https://www.howtogeek.com/233383/why-was-80-chosen-as-the-default-http-port-and-443-as-the-default-h ...
- HDU3488 Tour —— 二分图最大权匹配 KM算法
题目链接:https://vjudge.net/problem/HDU-3488 Tour Time Limit: 3000/1000 MS (Java/Others) Memory Limit ...
- YTU 2958: 代码填充--雨昕学画画
2958: 代码填充--雨昕学画画 时间限制: 1 Sec 内存限制: 128 MB 提交: 156 解决: 102 题目描述 雨昕开始学画水彩画,老师给雨昕一个形状(Shape)类,雨昕在Sha ...
- YTU 2928: 取不重复的子串。
2928: 取不重复的子串. 时间限制: 1 Sec 内存限制: 128 MB 提交: 5 解决: 5 题目描述 输入字母构成的字符串(不大于30字符)从中读取3个不重复的字符,求所有取法,取出的 ...