题目地址:24道CF的DIv2 CD题有兴趣可以做一下。
ACM思维题训练集合
Bajtek is learning to skate on ice. He’s a beginner, so his only mode of transportation is pushing off from a snow drift to the north, east, south or west and sliding until he lands in another snow drift. He has noticed that in this way it’s impossible to get from some snow drifts to some other by any sequence of moves. He now wants to heap up some additional snow drifts, so that he can get from any snow drift to any other one. He asked you to find the minimal number of snow drifts that need to be created.

We assume that Bajtek can only heap up snow drifts at integer coordinates.

Input
The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of snow drifts. Each of the following n lines contains two integers xi and yi (1 ≤ xi, yi ≤ 1000) — the coordinates of the i-th snow drift.

Note that the north direction coinсides with the direction of Oy axis, so the east direction coinсides with the direction of the Ox axis. All snow drift’s locations are distinct.

Output
Output the minimal number of snow drifts that need to be created in order for Bajtek to be able to reach any snow drift from any other one.

Examples
Input
2
2 1
1 2
Output
1
Input
2
2 1
4 1
Output
0

这道题做过但是不记得当时怎么做的了,每次做都有新的感受。

并查集,一般的并查集不太行,如图
代码

#include <bits/stdc++.h>
using namespace std;
int fa[105];
int  find(int n)
{if(fa[n]==n) return n;else return fa[n]=find(fa[n]);
}
struct Node{int x,y;
}node[105];
bool check(Node a,Node b)
{if(a.x==b.x) return 1;else if(a.y==b.y)return 1;else return 0;
}
set<int> cnt;
int main()
{int n;cin>>n;for(int i=0;i<n;i++){fa[i]=i;cin>>node[i].x>>node[i].y;for(int j=0;j<i;j++){if(check(node[i],node[j])){fa[find(j)]=find(i);// cout<<fa[j]<<endl;}}}for(int i=0;i<n;i++){//cout<<find(fa[i])<<endl;cnt.insert(find(i));}cout<<cnt.size()-1<<endl;
}

CF思维联系--CodeForces - 218C E - Ice Skating (并查集)相关推荐

  1. codeforces 400D Dima and Bacteria 并查集+floyd

    题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...

  2. Codeforces 699D Fix a Tree 并查集

    原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种 ...

  3. Codeforces 1140F 线段树 分治 并查集

    题意及思路:https://blog.csdn.net/u013534123/article/details/89010251 之前cf有一个和这个相似的题,不过那个题只有合并操作,没有删除操作,直接 ...

  4. CF思维联系– CodeForces - 991C Candies(二分)

    ACM思维题训练集合 After passing a test, Vasya got himself a box of n candies. He decided to eat an equal am ...

  5. CF思维联系–CodeForces - 225C. Barcode(二路动态规划)

    ACM思维题训练集合 Desciption You've got an n × m pixel picture. Each pixel can be white or black. Your task ...

  6. CF思维联系–CodeForces -224C - Bracket Sequence

    ACM思维题训练集合 A bracket sequence is a string, containing only characters "(", ")", ...

  7. CF思维联系–CodeForces - 223 C Partial Sums(组合数学的先线性递推)

    ACM思维题训练集合 You've got an array a, consisting of n integers. The array elements are indexed from 1 to ...

  8. CF思维联系–CodeForces - 222 C Reducing Fractions(数学+有技巧的枚举)

    ACM思维题训练集合 To confuse the opponents, the Galactic Empire represents fractions in an unusual format. ...

  9. CF思维联系--CodeForces -214C (拓扑排序+思维+贪心)

    ACM思维题训练集合 Furik and Rubik love playing computer games. Furik has recently found a new game that gre ...

最新文章

  1. Pythonic版冒泡排序和快速排序(附:直接插入排序)
  2. 包画三维散点图怎么导出_不开玩笑,发现宝藏了,一键导出场地三维模型,连贴图都有!...
  3. DDoS攻击可能损害企业品牌的四种方式
  4. 【详细讲解】某书店有一个收银员该书店最多允许n个购书者进入。将收银员和购书者看作不同的进程,其工作流程如下图所示。利用PV操作实现该过程,设置信号量S1,S2和Sn,初值分别为0,0,n.则图中a1
  5. 加密机组会 会议纪要
  6. html5手机移动端三级联动城市选择器
  7. LeetCode 1926. 迷宫中离入口最近的出口(BFS)
  8. (数据库系统概论|王珊)第九章关系查询处理和关系优化-第三节:查询优化之代数优化
  9. Python内置函数——__import__ 的使用方法
  10. 东大oj-1591 Circle of friends
  11. java技术面试之面试题大全
  12. 没想到还可以用地图这么讲故事?Esri产品布道师为你精细了18个应用案例
  13. Onenbsp;ornbsp;morenbsp;scree…
  14. shiny | 使用R创建一个网页应用(Web App)
  15. ps2017 cc 打印排版一寸照片
  16. 专访Polychain创始人Olaf:我们只囤币,从不做空
  17. js通用判断指定日期是周几,是否是周六、周日
  18. 常见的HTTP Headers有哪些?
  19. ns3 Traffic Control Layer解读
  20. 服务器文件ansi,ansi编码文件在服务器上显示乱码

热门文章

  1. 服务器设备日常维护与管理论文,浅谈设备管理与维护论文
  2. C语言家谱管理程序,课内资源 - 基于C语言实现的家谱管理系统
  3. You must supply a resourceID for a TextView
  4. git 修改本地用户名_git简单介绍
  5. Win10系统,安装虚拟机,是微软自家的 Hyper-V ?还是轻量级的 Virtual Box ?
  6. C++11 作用域内枚举
  7. Incompatible JavaHL library loaded. Subversion 1.8.x required.
  8. Postgres客户端编码问题
  9. 模板:什么是Traits
  10. 实战Cisco路由器交换机各型号密码恢复