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. python3练习100题——002

    因为特殊原因,昨天没有做题.今天继续- 原题链接:http://www.runoob.com/python/python-exercise-example2.html 题目: 企业发放的奖金根据利润提 ...

  2. 我眼中的ASP.NET.MVC

    MVC MVC全名 : Model View Controller ( Model-模型 View-视图  Controller-控制器)是一种经典的,经久不衰的,屹立不倒的软件设计框架.实现了业务逻 ...

  3. 根据ip列表模拟输出redis cluster的主从对应关系

    需求:给点一批ip列表,一个数组或者一个文件,每行一个ip,模拟输出redis cluster的组从关系,前者是master_ip:master_port -> slave_ip:slave_p ...

  4. 剑指offer-面试题38-字符串的排列-全排列

    /* 题目: 输入字符串,打印字符串的所有排列. 输入acc,输出[acc, cac, cca]. */ /* 思路: 将字符串看作两部分,第一个字符串和后面的部分. 将第一个字符串与后面字符串依次交 ...

  5. Java使用POI读取Word中的表格

    个人博客 地址:https://www.wenhaofan.com/a/20190627135921 代码 package live.autu.word; import java.io.FileInp ...

  6. MySQL 8 日期计算

    timestampdiff()函数: 按照需要的单位计算两值之间的时间差,比如: select timestampdiff(year, date_1, date_2) from table_name; ...

  7. Redis入门-01

    目录 使用场景 支持的数据类型 主从复制 原理 配置 哨兵机制 持久化 RDB(Redis Database) AOF(Append Only File) redis(Remote DIctionar ...

  8. 到底什么是.NET?

    ​.NET 概念比较庞大,本文只讨论基础知识,只用简单语言描述. 我们是NET程序员,但是我们有没有思考过到底什么是.NET ? 官方定义 .NET是微软推出来的一个致力于敏捷开发的软件框架. 大概2 ...

  9. [Python机器学习]机器学习概述

    1.为何选择机器学习 在智能应用的早期,许多系统使用人为的if和else语句来处理数据,以主动拦截邮箱的垃圾邮件为例,可以创建一个关键词黑名单,所有包含这些关键词的邮件被标记为垃圾邮件,这是人为制定策 ...

  10. 树莓派3B从装系统到安装RYU过程

    1.首先安装的是2019-09-26-raspbian-buster-lite系统 2.更换成国内源 更改apt源 sudo nano /etc/apt/sources.list(更改apt源) de ...