Ice Skating
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.
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 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.
2
2 1
1 2
1
2
2 1
4 1
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的更多相关文章
- CF 217A Ice Skating
A. Ice Skating time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces K. Ice Skating(求强连通分量)
题目描述: Ice Skating time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- CF思维联系--CodeForces - 218C E - Ice Skating (并查集)
题目地址:24道CF的DIv2 CD题有兴趣可以做一下. ACM思维题训练集合 Bajtek is learning to skate on ice. He's a beginner, so his ...
- News common vocabulary
英语新闻常用词汇与短语 经济篇 accumulated deficit 累计赤字 active trade balance 贸易顺差 adverse trade balance 贸易逆差 aid 援助 ...
- Mango Weekly Training Round #3 解题报告
A. Codeforces 92A Chips 签到题.. #include <iostream> #include <cstdio> #include <cstring ...
- Codeforces Round #134 (Div. 2)
A. Mountain Scenery 枚举山顶位置,满足\(r_{i-1} \lt r_i - 1 \gt r_{i+1}\). 范围要开\(2N\). B. Airport 优先队列维护最值. C ...
- 主流H.264编码器对比测试 (MSU出品)
俄罗斯的MSU Graphics & Media Lab (Video Group)出品的H.264编码器性能测试报告.测试了主流的H.264编码器的性能.从测试的结果来看,开源产品x264性 ...
- 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 ...
- 牛客练习赛16 C 任意点【并查集/DFS/建图模型】
链接:https://www.nowcoder.com/acm/contest/84/C 来源:牛客网 题目描述 平面上有若干个点,从每个点出发,你可以往东南西北任意方向走,直到碰到另一个点,然后才可 ...
随机推荐
- C++ const和constexpr
const expression , 常量表达式 , 在<C++ Primer>的定义:值不会改变并且在编译过程就能得到计算结果的表达式. 它要求两点:值不会改变,编译过程得到结果. ...
- Python中not、and、or的优先级
优先级:not > and > or 1.not与紧跟其后的那个条件是不可分割的2.如果条件语句全部由纯and.或纯or链接,按照从左到右的顺序依次计算即可 print(True and ...
- 在Windows7中的各种显示模式中桌面图标的尺寸
在Windows7中的各种显示模式中,图标的尺寸 window7 桌面icon设计尺寸大小桌面图标设计尺寸一般是多少超大图标:256X256大图标:128X128中图标:32X32平铺:32X32列表 ...
- 一、JVM之类加载器
一.什么是JVM 先来看下百度百科的解释: JVM 是 Java Virtual Machine(Java 虚拟机)的缩写,JVM 是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计 ...
- 关于iScroll在安卓移动端/chrome模拟移动端上下滑动卡顿问题处理!!!!真实可靠!!!已解决!!!
滑动卡顿效果 安卓手机打开微信浏览网页,Chrome模拟手机浏览网页,都出现的问题滑动卡顿! 修改代码点: 1. <style type="text/css"> ...
- Spark学习之路 (二十三)SparkStreaming的官方文档[转]
SparkCore.SparkSQL和SparkStreaming的类似之处 SparkStreaming的运行流程 1.我们在集群中的其中一台机器上提交我们的Application Jar,然后就会 ...
- 谷歌 AI 负责人谈2020 年机器学习趋势:多任务和多模态会有大突破
在上周加拿大温哥华举行的NeurIPS会议上,机器学习成为了中心议题. 来自世界范围内约1.3万名研究人员集中探讨了神经科学.如何解释神经网络输出以及人工智能如何帮助解决现实世界中的重大问题等焦点话 ...
- HDU5608
题意 英文 做法 设\(g(n)=n^2-3n+2\),有\(g(n)=\sum\limits_{d|n}f(d)\),反演一下有\(f(n)=\sum\limits_{d|n}\mu(\frac{n ...
- LeetCode 728. 自除数
题目链接:https://leetcode-cn.com/problems/self-dividing-numbers/ 给定上边界和下边界数字,输出一个列表,列表的元素是边界(含边界)内所有的自除数 ...
- Linux vim三种模式的快捷键
1.移动光标 数字 + h,j,k,l 上,下,左,右 ctrl-e 移动页面 ctrl-f 上翻一页 ctrl-b 下翻一页 ctrl-u 上翻半页 ctrl-d 下翻半页 w 跳到下一个字首,按标 ...