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 来源:牛客网 题目描述 平面上有若干个点,从每个点出发,你可以往东南西北任意方向走,直到碰到另一个点,然后才可 ... 
随机推荐
- Elasticsearch配置集群环境
			环境选择: 1.方案一:准备三台机器 每一台机器一个节点 2.方案二:准备一台机器 启动三个节点,用端口号区分即可 3.ES启 ... 
- 【MVC+EasyUI实例】对数据网格的增删改查(下)
			前言 继上文对网格加载数据,本文主要阐述对数据增删改的实现. 一.js代码 function Add() { $("#dlg").dialog('open'); $("# ... 
- 离线安装PostgreSQL11.6
			因为客户最近有一台CentOS7的虚拟机,但是没有联网,需要安装离线安装PostgreSQL 1.首先去官网下载离线安装包 https://www.postgresql.org/download/ 说 ... 
- W25Q64BV(FLASH)(SPI)中文手册
			64兆位串行SPI FLASH存储器 1.常规介绍 W25Q64BV(64兆位)串行FLASH存储器为一个空间大小,引脚,功耗限制的系统提供解决方案.25Q系列的灵活性和性能良好超越了普通的串行FLA ... 
- 吴裕雄--天生自然HADOOP操作实验学习笔记:hbase的javaAPI应用
			实验目的 进一步了解hbase的操作 熟悉使用IDEA进行java开发 熟悉hbase的javaAPI 实验原理 前面已经了解通过hbase的shell操作hbase,确实比较难以使用,另外通过hiv ... 
- python报错bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml.
			qpython运行 原代码: soup = BeautifulSoup(r.text,'lxml') 报错:bs4.FeatureNotFound: Couldn't find a tree b ... 
- Java不同单词个数统计
			描述 编写一个程序,输入一个句子,然后统计出这个句子当中不同的单词个数.例如:对于句子“one little two little three little boys”,总共有5个不同的单词:one, ... 
- Deepin Linux 升级wine应用
			前提是升级已经安装的wine应用 参考: 微信升级 mkdir /tmp/wechat cd /tmp/wechat wget https://dldir1.qq.com/weixin/Windows ... 
- UVA1395 (最苗条的最小生成树)
			链接 https://vjudge.net/problem/UVA-1395 代码 #include<bits/stdc++.h> using namespace std; #define ... 
- 安装Logstash到linux(源码)
			运行环境 系统版本:CentOS Linux release 7.3.1611 (Core) 软件版本:logstash-7.1.0 硬件要求:最低2核4GB 安装过程 1.源码安装JDK 1.1.从 ... 
