并查集Codeforces Round #134 (Div. 1), problem: (A) Ice Skating
来源:互联网 发布:解压缩包的软件 编辑:程序博客网 时间:2024/04/19 22:57
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.
22 11 2
1
22 14 1
0
题意:一个人初学滑雪,他只会上下左右沿直线滑雪,下面有n个坐标,给出这些休息地点,同一行或者同一列的话,他就可以滑过去休息,但是可能还会有一些地点他滑不过去问至少还要再建几个休息的地点,他才可以全部滑到.
分析:这个题目就是典型的并查集了,判断两个点在同一行或者同一列就可以连在一起,最后看有几个根节点就好了
下面贴出代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int father[105];struct point{ int x,y;}p[105];void init(){ int i; for(i=0;i<105;i++) father[i]=i;}int find_father(int x){ return x==father[x]?x:father[x]=find_father(father[x]);}void union_tree(int x,int y){ father[find_father(x)]=father[find_father(y)];}int main(void){ int i,j,n,m,cnt; while(scanf("%d",&n)!=EOF){ init(); cnt=0; for(i=0;i<n;i++) scanf("%d %d",&p[i].x,&p[i].y); for(i=0;i<n;i++){ for(j=i+1;j<n;j++){ if(p[j].x==p[i].x||p[i].y==p[j].y){ union_tree(i,j); } } } for(i=0;i<n;i++) if(father[i]==i) cnt++; printf("%d\n",cnt-1); }}
- 并查集Codeforces Round #134 (Div. 1), problem: (A) Ice Skating
- CodeForces 217A Ice Skating(并查集思路)
- Codeforces 217A. Ice Skating 【并查集】
- CodeForces 217 A.Ice Skating(并查集)
- Codeforces Round #134 (Div. 2)C. Ice Skating
- Codeforces Round #134 (Div. 2) C. Ice Skating
- CF218C:Ice Skating(并查集)
- codeforces 217A Ice Skating
- Codeforces 217A: Ice Skating
- Codeforces Round #363 (Div. 2) Problem D - Fix a Tree(并查集 + 构造)
- Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem 开关--并查集
- CF 并查集(或者图) Ice Skating
- Codeforces 217A. Ice Skating 搜索
- Codeforces 217A Ice Skating 题解
- Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem 2-SAT、并查集
- Codeforces Round #250 (Div. 1) B 并查集
- Codeforces---Ice Skating
- Codeforces Round #446(Div.2)Problem E Envy(并查集)
- OpenGL阴影,Shadow Mapping
- 安卓相关资料网址
- ffmpeg2.2在ubuntu下使用NDK编译——并在android工程下测试使用
- Android--在Activity中使用Thread导致的内存泄漏
- linux开发基础问题答疑
- 并查集Codeforces Round #134 (Div. 1), problem: (A) Ice Skating
- 水晶报表教程:手把手教你制作基本报表
- Eclipse文本高亮
- C语言中一些不常用函数
- 项目BUG总结2
- ScrollView里包含GridView应注意的两个问题
- 小技巧:图片的呼吸效果
- C++读取OBJ文件中的数据(VS2013)
- Auto Layout 好的博客链接