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. python 程序构架浅析

    定义:通常的 Python 程序的构架是指:将一个程序分割为源代码文件的集合以及将这些部分连接在一起的方法. python的程序构架可表示为: 一个python程序就是一个模块的系统.它有一个顶层文件 ...

  2. HttpServletRequest修改/添加header和cookie参数

    实现功能: 所有接口经过过滤器,获取每个接口的自定义头部(token) 判断如果是app访问,则给头部设置cookie,值为自定义token的值. 即:使用过滤器实现修改请求头headers 实现步骤 ...

  3. 原理分析之一:从JDBC到Mybatis

    原理分析之一:从JDBC到Mybatis Mybatis学习(一)原生态的JDBC编程总结 -----系列 深入浅出MyBatis-快速入门

  4. Nginx负载均衡 ssl原理 生成ssl密钥对 Nginx配置ssl

  5. 小程序的tab切换事件

    index.wxml代码 <view class="tab-left" > <view " bindtap="tab">tab ...

  6. Android学习笔记——Intents 和 Intent Filters(二)

    本人邮箱:JohnTsai.Work@gmail.com,欢迎交流讨论. 欢迎转载,转载请注明网址:http://www.cnblogs.com/JohnTsai/p/3993488.html 知识点 ...

  7. Spring依赖注入原理分析

    在分析原理之前我们先回顾下依赖注入的概念: 我们常提起的依赖注入(Dependency Injection)和控制反转(Inversion of Control)是同一个概念.具体含义是:当某个角色( ...

  8. Blender 移动、旋转、缩放

    1.手动调整物体的属性 当我们添加了一个物体后,例如一个Torus物体. 在左侧下角部分能看到“Add Torus”面板,面板包含了Locatin.Rotation.Major Segments.Mi ...

  9. 设计模式之初识IoC/DI(六)

    本篇和大家一起学习IoC和DI即控制反转和依赖注入. 当然听上去这词语非常的专业,真不知道是怎么组出来的,看上去难归看上去难,但稍微理解一下也就这么回事了. 首先我们要明白IoC/DI干嘛用的,不然别 ...

  10. SQL SERVER发布与订阅

    一.配置分发 1.配置分发服务器,注:配置发布与订阅,连接SQLSERVER必须用服务器名登录 2.配置分发 3.选择分发服务器 4.选择快照文件夹 5.设置此文件夹的读写权限为everyone 6. ...