思路:

并查集的应用。

实现:

  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4.  
  5. bool a[][];
  6. int n, x, y;
  7. int par[];
  8. int ran[];
  9. int dx[] = { , , -, };
  10. int dy[] = { , , , - };
  11.  
  12. void init(int n)
  13. {
  14. for (int i = ; i < n; i++)
  15. {
  16. ran[i] = ;
  17. par[i] = i;
  18. }
  19. }
  20.  
  21. int find(int x)
  22. {
  23. if (par[x] == x)
  24. return x;
  25. return par[x] = find(par[x]);
  26. }
  27.  
  28. void unite(int x, int y)
  29. {
  30. x = find(x);
  31. y = find(y);
  32. if (x == y)
  33. return;
  34. if (ran[x] < ran[y])
  35. par[x] = y;
  36. else
  37. {
  38. par[y] = x;
  39. if (ran[x] == ran[y])
  40. ran[x]++;
  41. }
  42. }
  43.  
  44. bool same(int x, int y)
  45. {
  46. return find(x) == find(y);
  47. }
  48.  
  49. int trans(int x, int y)
  50. {
  51. return x * + y;
  52. }
  53.  
  54. int main()
  55. {
  56. init();
  57. cin >> n;
  58. int now = , c = ;
  59. for (int i = ; i < n; i++)
  60. {
  61. now++;
  62. c += ;
  63. cin >> x >> y;
  64. a[x][y] = true;
  65. int tmp = trans(x, y);
  66. for (int j = ; j < ; j++)
  67. {
  68. int nx = x + dx[j];
  69. int ny = y + dy[j];
  70. if (nx >= && nx < && ny >= && ny < && a[nx][ny])
  71. {
  72. int t = trans(nx, ny);
  73. if (!same(tmp, t))
  74. {
  75. unite(tmp, t);
  76. now--;
  77. }
  78. c -= ;
  79. }
  80. }
  81. cout << now << " " << i + << " " << c << endl;
  82. }
  83. return ;
  84. }

hihocoder offer收割编程练习赛11 C 岛屿3的更多相关文章

  1. hihocoder offer收割编程练习赛11 D 排队接水

    思路: 莫队算法+树状数组. 莫队算法的基本思想是对大量要查询的区间进行离线处理,按照一定的顺序计算,来降低复杂度.概括来说,我们在知道了[l, r]的解,并且可以通过一个较低的复杂度推出[l - 1 ...

  2. hihocoder offer收割编程练习赛11 B 物品价值

    思路: 状态压缩 + dp. 实现: #include <iostream> #include <cstdio> #include <cstring> #inclu ...

  3. hihocoder offer收割编程练习赛11 A hiho字符串

    思路: 我用的尺取. 注意题目描述为恰好2个'h',1个'i',1个'o'. 实现: #include <iostream> #include <cstdio> #includ ...

  4. hihocoder [Offer收割]编程练习赛4

    描述 最近天气炎热,小Ho天天宅在家里叫外卖.他常吃的一家餐馆一共有N道菜品,价格分别是A1, A2, ... AN元.并且如果消费总计满X元,还能享受优惠.小Ho是一个不薅羊毛不舒服斯基的人,他希望 ...

  5. hihocoder [Offer收割]编程练习赛61

    [Offer收割]编程练习赛61 A:最小排列 给定一个长度为m的序列b[1..m],再给定一个n,求一个字典序最小的1~n的排列A,使得b是A的子序列. 贪心即可,b是A的子序列,把不在b中的元素, ...

  6. 【[Offer收割]编程练习赛11 C】岛屿3

    [题目链接]:http://hihocoder.com/problemset/problem/1487 [题意] 中文题 [题解] 岛屿的数目对应了这个图中联通块的数目; 面积则对应有多少个方块; 周 ...

  7. ACM学习历程—Hihocoder [Offer收割]编程练习赛1

    比赛链接:http://hihocoder.com/contest/hihointerview3/problem/1 大概有一个月没怎么打算法了.这一场的前一场BC,也打的不是很好.本来Div1的A和 ...

  8. hihocoder offer收割编程练习赛8 C 数组分拆

    思路:(引自bfsoyc的回答:http://hihocoder.com/discuss/question/4160) 动态规划.状态dp[i]表示 前i个数的合法的方案数,转移是 dp[i] = s ...

  9. 【[Offer收割]编程练习赛11 B】物品价值

    [题目链接]:http://hihocoder.com/problemset/problem/1486 [题意] [题解] 设f[i][j]表示前i个物品,每种属性的状态奇偶状态为j的最大价值; 这里 ...

随机推荐

  1. VUE组件如何与iframe通信问题

    vue组件内嵌一个iframe,现在想要在iframe内获取父vue组件内信息,由于本人技术有限,采用的是H5新特性PostMessage来解决跨域问题. postMessage内涵两个API: on ...

  2. bzoj3109【CQOI2013】新数独

    3109: [cqoi2013]新数独 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 365  Solved: 229 [Submit][Statu ...

  3. PL/SQL Developer导入导出Oracle数据库方法

    前一篇博客介绍了Navicat工具备份Oracle的方法.这篇博客介绍一下使用PL/SQL Developer工具导入导出Oracle数据库的方法. PL/SQL Developer是Oracle数据 ...

  4. Codeforces 794F. Leha and security system 线段树

    F. Leha and security system   Bankopolis, the city you already know, finally got a new bank opened! ...

  5. Flame Graphs

    http://www.brendangregg.com/flamegraphs.html Flame graphs are a visualization of profiled software, ...

  6. WebService注解汇总

    Web Service 元数据注释(JSR 181) @WebService 1.serviceName: 对外发布的服务名,指定 Web Service 的服务名称:wsdl:service.缺省值 ...

  7. Node.js 101(2): Promise and async

    --原文地址:http://blog.chrisyip.im/nodejs-101-package-promise-and-async 先回想一下 Sagase 的项目结构: lib/ cli.js ...

  8. PR 修改保存的增强 ME_UPDATE_REQUISITION

    FUNCTION me_update_requisition."""""""""""&qu ...

  9. YTU 1010: 目标柏林

    1010: 目标柏林 时间限制: 1000 Sec  内存限制: 64 MB 提交: 32  解决: 15 题目描述 1945年初,苏军和英美联军已从东西两面攻入德国国境. 4月初,在苏军和英美联军的 ...

  10. YTU 2946: 填空:间接基类就是A

    2946: 填空:间接基类就是A 时间限制: 1 Sec  内存限制: 128 MB 提交: 132  解决: 96 题目描述 如下程序所示,D继承自B和C,而B和C均继承自A.根据继承的机制,D的对 ...