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 来源:牛客网 题目描述 平面上有若干个点,从每个点出发,你可以往东南西北任意方向走,直到碰到另一个点,然后才可 ...
随机推荐
- PHP0018:PHP 图像处理
- 洛谷新手题 P1028 数的计算题解
题目描述 我们要求找出具有下列性质数的个数(包含输入的自然数nn): 先输入一个自然数nn(n \le 1000n≤1000),然后对此自然数按照如下方法进行处理: 不作任何处理; 在它的左边加上一个 ...
- Android实战项目——家庭记账本(五)
今天博客写的有点晚(好像算是昨天的了),有一点小bug刚刚改完.今天完成的任务有: 1.统计页的布局和功能 2.主页碎片的图表功能 实现效果如下: 其中,统计 ...
- 如何在CentOS上安装Tensorflow的gpu版本?
系统配置 系统版本: Centos7.6 语言: Python3.5(anaconda3 4.2) 框架: Tensorflow 安装依赖 sudo yum install openjdk-8-jdk ...
- Lucene搜索引擎入门
一.什么是全文检索? 就是在检索数据,数据的分类: 在计算机当中,比如说存在磁盘的文本文档,HTML页面,Word文档等等...... ...
- 使用INF创建CSR文件
公司要为一个英国的客户提供由HTTP升级到HTTPS的服务,于是接触到了申请SSL证书这方面的内容. 一.总的来说,申请证书需要两步,一是创建CSR文件,二是在证书提供商购买证书并将CSR文件发给证书 ...
- Jquery实现挂号平台首页源码2
第二个版本:点击预约挂号可跳转到排班表,获取之后7个星期的排班 先放图 首先是index.html <!DOCTYPE html> <html lang="en" ...
- mysql 零基础 开始过程
2016-11-07 算是差不多是两个月的学习,我也马上要结课了.今天才勉强把mysql装上,之前还有因为用户权限的问题,以及用户为创建的问题.应该早点就把该准备的开发平台准备好,而不是在最后快考试了 ...
- 嵊州D2T4 十七个中毒的英国人 poisoning
嵊州D2T4 十七个中毒的英国人 poisoning 利内罗女士准备来到意大利进行修行. 意大利由 n 个城市和 m 条道路构成,道路是双向的. 到达第 i 个城市时,她可以取得该城市的全部信仰,并获 ...
- 《京东B2B业务架构演变》阅读
转载:https://mp.weixin.qq.com/s/5xmmuw8O-I_Fi5bzE-_baA?tdsourcetag=s_pcqq_aiomsg 京东 B2B 业务的定位是让各类型的企业都 ...