CF 217A Ice Skating
2 seconds
256 megabytes
standard input
standard output
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
【题意】
给出n个点的横、纵坐标(在一条直线上的可以相互到达),问至少再加几个点可以使得所有的点之间可以通过上下左右走互相到达,(拐点上必须有“点”)
【分析】
若两个点横坐标或者纵坐标相同,两点间连一条边 通过dfs统计连通块的个数
假设处理完后有t个缩点,只需t-1条边就可将其变成一个缩点。故答案为t-1.
当然并查集也是可以的,但dfs更轻便
【代码】
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
typedef pair<int,int> pir;
const int N=105;
int n,ans;pir e[N];bool vis[N],g[N][N];
void dfs(int x){
vis[x]=1;
for(int j=1;j<=n;j++){
if(!vis[j]&&g[x][j]){
dfs(j);
}
}
}
#define x first
#define y second
inline void Solve(){
for(int i=1;i<=n;i++){
for(int j=1;j<i;j++){
if(e[i].x==e[j].x||e[i].y==e[j].y){
g[i][j]=g[j][i]=1;
}
}
}
for(int i=1;i<=n;i++) if(!vis[i]) dfs(i),ans++;
printf("%d",ans-1);
}
inline void Init(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d%d",&e[i].x,&e[i].y);
}
int main(){
Init();
Solve();
return 0;
}
CF 217A Ice Skating的更多相关文章
- 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 ...
- Ice Skating
Bajtek is learning to skate on ice. He's a beginner, so his only mode of transportation is pushing o ...
- Mango Weekly Training Round #3 解题报告
A. Codeforces 92A Chips 签到题.. #include <iostream> #include <cstdio> #include <cstring ...
- 牛客练习赛16 C 任意点【并查集/DFS/建图模型】
链接:https://www.nowcoder.com/acm/contest/84/C 来源:牛客网 题目描述 平面上有若干个点,从每个点出发,你可以往东南西北任意方向走,直到碰到另一个点,然后才可 ...
- ACM思维题训练 Section A
题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...
- News common vocabulary
英语新闻常用词汇与短语 经济篇 accumulated deficit 累计赤字 active trade balance 贸易顺差 adverse trade balance 贸易逆差 aid 援助 ...
- 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性 ...
随机推荐
- UpdateData()用法
一.总结UpdateData()函数 UpdateData(true);//用于将屏幕上控件中的数据交换到变量中. UpdateData(false);//用于将数据在屏幕中对应控件中显示出来. ...
- [译]Angular-ui 之 Url Routing
◄ 前一篇 (Multiple Named Views) 下一篇 (The Components) ► 在你的应用中多数的状态都是基于特定的url地址的.Url Routing机制绝不是在状态 ...
- UNIX环境编程学习笔记(11)——文件I/O之文件时间以及 utime 函数
lienhua342014-09-16 1 文件的时间 每个文件都有三个时间字段,如表 1 所示. 表 1: 文件的三个时间字段 说明 字段 st_atime 文件数据的最后访问时间 st_mtime ...
- 无插件用Terminal/TotalTerminal的开当前finder位置
从win7开始,在资源管理器内按住shift再右键,是可以选择“在当前位置打开命令行”的,相当有用,这个命令在mac下是如何实现的呢? 前提:我讲的是TotalTerminal下的方案,原生的term ...
- SpringBoot实战(四)之使用JDBC和Spring访问数据库
这里演示的是h2databse示例,所以简单的介绍普及下h2database相关知识 H2数据库是一个开源的关系型数据库. H2是一个嵌入式数据库引擎,采用java语言编写,不受平台的限制,同时H2提 ...
- JSP判断闰年
闰年条件: 能被4整除但不能被100整除 能被400整除 <%@ page language="java" import="java.util.*" co ...
- 五步整理你的css文件
鉴于实在无法忍受那种写一句就换一行的css写法,有个项目中的一个css文件竟然高达6000多行,看着实在蛋疼,无实今天下定决心整理一下,在DW里可以用正则很好的进行替换,步骤如下: 一:\r => ...
- pytesseract 报windows err no2的错误
需要把源安装文件pytesseract.py的修改为,tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract.exe' 原始是t ...
- NHibernate 集合映射深入 (第五篇) <set>,<list>,<map>,<bag>
一.集合外键 在NHibernate中,典型的用于映射集合类的元素有<set>,<list>,<map>,<bag>,<array>,< ...
- 03-Linux各目录及每个目录的详细介绍
Linux各目录及每个目录的详细介绍 [常见目录说明] 目录 /bin 存放二进制可执行文件(ls,cat,mkdir等),常用命令一般都在这里. /etc 存放系统管理和配置文件 /home 存放所 ...