随笔(并查集)
来源:互联网 发布:access数据库查看器 编辑:程序博客网 时间:2024/05/17 18:00
今天的比赛真的好郁闷啊!一个博弈题弄了两小时结果还WA啦,放下博弈接着写并查集,这是一道比较简单的题目。由于输入数据有多组,而我只用一组处理导致很悲剧的无数次WA。。。。伤心ING ...
题目:
Statistics
Ice Skating
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 8 Accepted Submission(s) : 3
Font: Times New Roman | Verdana | Georgia
Font Size:← →
Problem Description
LZ 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 LZ can only heap up snow drifts at integer coordinates.
Input
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.
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
Output the minimal number of snow drifts that need to be created in order for LZ to be able to reach any snow drift from any other one.
Sample Input
22 11 2
Sample Output
1
代码:
#include <iostream> #include<cstdio> #include<string.h> using namespace std; struct node { int x,y; }q[110]; int father[110]; int find(int x) { if(x!=father[x]) father[x]=find(father[x]); return father[x]; } int main() { int n,i,j,k,num = 0; //freopen("Din.txt","r",stdin); //freopen("ou.txt","w",stdout); while(scanf("%d",&n)!=EOF) { num=0; for(i = 1; i <= n ;i++) { scanf("%d%d",&q[i].x,&q[i].y); } for(i = 1; i <= n ;i++) father[i] = i; for(i = 1; i < n ; i++) { for(j = i+1; j <= n ;j++) { if(q[j].x==q[i].x||q[j].y==q[i].y) { int px = find(i); int py = find(j); if(px!=py); father[px]=py; } } } for(i=1;i<=n;i++) if(father[i]==i) num++; printf("%d\n",num-1); } return 0; }
- 随笔(并查集)
- 并查集(集并查)
- 并查集(含关系并查集)
- 【并查集】食物链(关系并查集)
- 【并查集】并查集详解(转)
- 分组并查集(种类并查集)
- 并查集, 畅通工程(简单并查集)
- [数据结构]并查集水体POJ2236(并查集)
- 并查集(disjoint_set)
- 并查集(模板)
- 并查集(亲戚)
- POJ2524(并查集)
- POJ_2236(并查集)
- poj2492(并查集)
- HDU1272(并查集)
- POJ1988(并查集)
- hdoj1272(并查集)
- 家族(并查集)
- 关于broadcast receiver
- c#中两个DateTimePicker,一个时间设置为0:0:0,另一个设置为23:59:59
- DB2 For AS400 字符函数
- win7安装telnet
- 在设备上开启telnet服务
- 随笔(并查集)
- 临界区(Critical section)与互斥体(Mutex)的区别
- 数据库可疑问题处理
- java.io.FileNotFoundException: Template /template/SEL00/token.ftl not found. <s:token>出错
- hadoop里的MaxTemperature程序
- 什么是Shell
- 黑马程序员11-7k面试题交通灯
- linux c获取系统网卡信息
- 几种难度比较大的C宏定义