A. Ice Skating
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Copy
2
2 1
1 2
output

Copy
1
input

Copy
2
2 1
4 1
output

Copy
0

【题意】

给出n个点的横、纵坐标(在一条直线上的可以相互到达),问至少再加几个点可以使得所有的点之间可以通过上下左右走互相到达,(拐点上必须有“点”)

 

【分析】

若两个点横坐标或者纵坐标相同,两点间连一条边 通过dfs统计连通块的个数
假设处理完后有t个缩点,只需t-1条边就可将其变成一个缩点。故答案为t-1.
当然并查集也是可以的,但dfs更轻便

 


【代码】

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
typedef pair<int,int> pir;
const int N=105;
int n,ans;pir e[N];bool vis[N],g[N][N];
void dfs(int x){
vis[x]=1;
for(int j=1;j<=n;j++){
if(!vis[j]&&g[x][j]){
dfs(j);
}
}
}
#define x first
#define y second
inline void Solve(){
for(int i=1;i<=n;i++){
for(int j=1;j<i;j++){
if(e[i].x==e[j].x||e[i].y==e[j].y){
g[i][j]=g[j][i]=1;
}
}
}
for(int i=1;i<=n;i++) if(!vis[i]) dfs(i),ans++;
printf("%d",ans-1);
}
inline void Init(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d%d",&e[i].x,&e[i].y);
}
int main(){
Init();
Solve();
return 0;
}

CF 217A Ice Skating的更多相关文章

  1. Codeforces K. Ice Skating(求强连通分量)

    题目描述: Ice Skating time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  2. CF思维联系--CodeForces - 218C E - Ice Skating (并查集)

    题目地址:24道CF的DIv2 CD题有兴趣可以做一下. ACM思维题训练集合 Bajtek is learning to skate on ice. He's a beginner, so his ...

  3. Ice Skating

    Bajtek is learning to skate on ice. He's a beginner, so his only mode of transportation is pushing o ...

  4. Mango Weekly Training Round #3 解题报告

    A. Codeforces 92A Chips 签到题.. #include <iostream> #include <cstdio> #include <cstring ...

  5. 牛客练习赛16 C 任意点【并查集/DFS/建图模型】

    链接:https://www.nowcoder.com/acm/contest/84/C 来源:牛客网 题目描述 平面上有若干个点,从每个点出发,你可以往东南西北任意方向走,直到碰到另一个点,然后才可 ...

  6. ACM思维题训练 Section A

    题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...

  7. News common vocabulary

    英语新闻常用词汇与短语 经济篇 accumulated deficit 累计赤字 active trade balance 贸易顺差 adverse trade balance 贸易逆差 aid 援助 ...

  8. Codeforces Round #134 (Div. 2)

    A. Mountain Scenery 枚举山顶位置,满足\(r_{i-1} \lt r_i - 1 \gt r_{i+1}\). 范围要开\(2N\). B. Airport 优先队列维护最值. C ...

  9. 主流H.264编码器对比测试 (MSU出品)

    俄罗斯的MSU Graphics & Media Lab (Video Group)出品的H.264编码器性能测试报告.测试了主流的H.264编码器的性能.从测试的结果来看,开源产品x264性 ...

随机推荐

  1. phd文献阅读日志-博一上学期

    为了记住并提醒自己阅读文献,进行了记录(这些论文都是我看过理解的),论文一直在更新中. 博一上学期: 1.week 6,2017.10.16 2014-Automatic Semantic Model ...

  2. 相对和绝对路径/cd命令/创建和删除目录mkdir/rmdir/rm命令

    2.6 相对和绝对路径 2.7 cd命令 2.8 创建和删除目录mkdir/rmdir 2.9 rm命令 绝对路径:从根开始的路径:文件所在的路径: 相对路径:相对于当前目录而言的路径:上一级或者下一 ...

  3. PHP多文件上传操作

    在前一篇文章里讲到了关于PHP文件上传原理和简单操作举例是单文件上传. http://www.cnblogs.com/lichenwei/p/3879566.html 其实多文件上传和单文件上传大同小 ...

  4. Visual Studio 2012中使用GitHub

    前言 一直以来都想使用Git来管理自己平时积累的小代码,就是除了工作之外的代码了.有时候自己搞个小代码,在公司写了,就要通过U盘或者网盘等等 一系列工具进行Copy,然后回家才能继续在原来的基础上作业 ...

  5. requireJs压缩合并路径问题

    描述 随着前端开发的重要性,以及业务的复杂性,前端的模块化开发也被大众所接收,最常见的js框架requireJs,一个js文件对应一个模块,方便开发人员调试与维护,但是一个文件对应一个模块增加了htt ...

  6. pycharm 对代码做静态检查

    对于下面这种情况,java c这些提前编译的语言,不给你运行机会就立马报错了,但对于动态语言运行之后才能报错,用运行的方法来检查代码错误是在是太坑了,这是py对比静态语言的巨大劣势,尤其是代码文件多行 ...

  7. SpringBoot------全局异常捕获

    1.添加异常类 package myshop.config; import javax.servlet.http.HttpServletRequest; import org.springframew ...

  8. Ubuntu Git安装与使用

    本系列文章由 @yhl_leo 出品.转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50760140 本文整理和归纳了关于Ub ...

  9. Java -- 异常的捕获及处理 -- Java的异常处理机制

    7.1.4 Java的异常处理机制 在整个Java的异常处理中,实际上也是按照面向对象的方式进行处理,处理的步骤如下: ⑴ : 一旦产生异常,则首先会产生一个异常类的实例化对象. ⑵ : 在try语句 ...

  10. swift开发之--代理协议的使用

    swift代理的使用,和oc版本有区别,区别还是蛮大的,不过和oc一样都是用于反向传值: 实现如下: 1,声明两个类 2,实现流程,viewcontroller页面点击按钮进入firstVC页面,然后 ...