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性 ...
随机推荐
- ngx-bootstrap学习笔记(一)-popover
前言 这月做了个ng2模块,其中有个校验功能,当校验不通过时给出提示,项目中使用jQuery实现,今天才发现ngx-bootstrap已经有现成功能了,且可封装成通用组件放入shareModule,使 ...
- Thinkphp5 iis环境下安装报错400 500
要求一:服务器需要开启伪静态功能 要求二:新建文件夹web.config 放到入口目录下(如public/web.config 或者/web.config),内容如: <?xml versio ...
- BareTail大文件日志实时查看工具
BareTail 动态的查看日志文件,就像Linux上的tail tail -f nohup.out 功能: 实时文件查看 tail命令模式,自动滚动 支持2g以上大文件 自动滚动 彩色监控 多文件监 ...
- 使用apache的poi实现导入导出excel
1.jar包:poi-3.14-20160307.jar.poi-ooxml-3.14-20160307.jar 2.导入(本例实现了解析excel生成List): @Override public ...
- 用VS2012不能打开VS2010的项目
应该是装过sql2012或者sql2008,自带的那个visual studio,你可以直接在外面打开VS2012,然后用里面的文件去打开项目.而不要直接双击那个解决方案的文件.
- Sql server连接数据库报错相关
情况一:此版本的 SQL Server 不支持用户实例登录标志. 解决方法: 方法1:在连接属性的设置里边,点高级,将User Instance 设置为false,默认的true(我在中没有找到相应的 ...
- jquery.fileupload插件 ie9下不支持上传
根据https://github.com/blueimp/jQuery-File-Upload/wiki/Browser-support The following browsers support ...
- docker开启api端口,docker启用加速
此篇是针对centos6的docker,注意ubantu和centos7的会有区别. 需要在docker配置文件中修改信息 centos中是这个文件 /etc/sysconfig/docker,Ubu ...
- C#(少用的)
挖一挖C#中那些我们不常用的东西之系列(1)——ToDictionary,ToLookup 挖一挖C#中那些我们不常用的东西之系列(2)——IsXXX 系列方法 挖一挖C#中那些我们不常用的东西之系列 ...
- 在SSH框架中,如何得到POST请求的URL和参数列表
在做项目的API通知接口的时候,发现在SSH框架中无法获取到对方服务器发来的异步通知信息.最后排查到的原因可能是struts2对HttpServletRequest进行了二次处理,那么该如何拿到pos ...