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

遍历每个连通的结点,如果遇到没有连通的结点那么添加一个结点即可将它并入连通图中。

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
//#include <xfunctional>
#define ll long long
#define mod 998244353
using namespace std;
int dir[][] = { {,},{,-},{-,},{,} };
const long long inf = 0x7f7f7f7f7f7f7f7f;
const int INT = 0x3f3f3f3f; int n, ans = ;
vector<vector< bool>> vis(,vector<bool>(,false));
void dfs(int x,int y)
{
vis[x][y] = false;
for (int i = ; i < ; i++)
{
if (vis[i][y])
dfs(i, y);
}
for (int i = ; i < ; i++)
{
if (vis[x][i])
dfs(x, i);
}
}
int main()
{
cin >> n;
while(n--)
{
int x, y;
cin >> x >> y;
vis[x][y] = true;
}
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
if (vis[i][j])
{
ans++;
dfs(i, j);
}
}
}
cout << ans - ;
return ;
}

Ice Skating的更多相关文章

  1. CF 217A Ice Skating

    A. Ice Skating time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

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

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

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

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

  4. News common vocabulary

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

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

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

  6. Codeforces Round #134 (Div. 2)

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

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

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

  8. English trip V2 - 6 Sports Teacher:Taylor Key:phrasal verbs

    In this lesson you will learn to talk about sports. 课上内容(Lesson) # How many different sports can you ...

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

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

随机推荐

  1. 简述react、redux、react-redux、redux-saga、dva之间的关系

    [react] 定位:React 是一个用于构建用户界面的JavaScript库. 特点:它采用声明范式来描述应用,建立虚拟dom,支持JSX语法,通过react构建组件,能够很好的去复用代码: 缺点 ...

  2. grep 基本用法

    grep usage: grep [options]... pattern [file]... 如果file缺省,会从 stdin 读取 $ grep --help -i 忽略大小写 -n 显示行号 ...

  3. python_函数笔记

    第二章 函数编程 定义: 函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可 特性: 减少重复代码 使程序变的可扩展 使程序变得易维护 形参变量 只有在被调 ...

  4. Bash脚本编程学习笔记04:测试命令test、状态返回值、位置参数和特殊变量

    我自己接触Linux主要是大学学习的Turbolinux --> 根据<鸟哥的Linux私房菜:基础篇>(第三版) --> 马哥的就业班课程.给我的感觉是这些课程对于bash的 ...

  5. vim编辑器未正常关闭时解决方案

    目录 vim编辑器未正常关闭时解决方案 问题描述 .swp..swo文件产生原因 解决方案 后记 hosts文件 sudo命令小记 vim编辑器未正常关闭时解决方案 问题描述 在mac上的/etc目录 ...

  6. Winform中怎样对窗体进行隐藏,再次打开时仍然保留上次的窗体

    场景 点击按钮后打开窗口,点击窗口的确定按钮后即使窗体返回了Ok,此时不关闭窗体,将窗体隐藏. 再次点击按钮后,仍然打开上次的窗体. 注: 博客主页: https://blog.csdn.net/ba ...

  7. Linux系统下的CPU、内存、IO、网络的压力测试

    本文转载自:小豆芽博客 一.对CPU进行简单测试: 1.通过bc命令计算特别函数 例:计算圆周率 echo "scale=5000; 4*a(1)" | bc -l -q MATH ...

  8. oracle基础知识点

    一.count(*).count(1).count(字段名)的区别select count(*) from t_md_inst --153797 --包含字段为null 的记录select count ...

  9. LOJ#508. 「LibreOJ NOI Round #1」失控的未来交通工具

    题意 一个带边权无向图,有两种操作:加边以及询问在\(x,x+b,...,x+(c-1)b\)这些数中,有多少个数存在至少一条与之模\(m\)同余的从\(u\)到\(v\)的路径(可以不是简单路径). ...

  10. 针对mysql8.0报错:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create

    折腾了好久,后来发现是版本问题,驱动和数据库不匹配导致. 原来用的是5.1.37的驱动.数据库是mysql5.7,可以连接成功. 就在我把数据库换成了8.0之后,所有的买点啥都报标题里的错误了.   ...