关闭

Codeforces 217A. Ice Skating 【并查集】

标签: codeforces数据结构
330人阅读 评论(0) 收藏 举报
分类:

A. Ice Skating
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.

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.

Output

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.

Examples
input
2
2 1
1 2
output
1
input
2
2 1
4 1
output
0

/*
    题意:给出n个点的坐标,如果两个点x相同或者y相同,则两点可以联通,
          问你最少加几条线,能使全部点联通
    类型:并查集
    分析:属于裸并查集了吧..
*/

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int n;
int fa[1005];
struct Node{
    int x,y;
};
Node a[1005];
int Find(int x){
    if(fa[x]==x)return x;
    return fa[x]=Find(fa[x]);
}
void unite(int x,int y){
    x=Find(x);
    y=Find(y);
    if(x!=y)fa[x]=y;
}
int main()
{
    while(~scanf("%d",&n)){
        for(int i=0;i<=n;i++)fa[i]=i;
        for(int i=0;i<n;i++)
            scanf("%d%d",&a[i].x,&a[i].y);
        for(int i=0;i<n;i++)
           for(int j=0;j<n;j++){
              if(a[j].x==a[i].x||a[j].y==a[i].y){
                  unite(i,j);
              }
           }
        int res=0;
        for(int i=0;i<n;i++){
            if(fa[i]==i)res++;
        }
        printf("%d\n",res-1);
    }
    return 0;
}





0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

HDU 4031 Attack(树状数组修改区间查询点)

Problem Description Today is the 10th Annual of “September 11 attacks”, the Al Qaeda is about to at...
  • u011217342
  • u011217342
  • 2014-01-21 18:09
  • 1096

ICE集群──负载均衡及容错

Zero ICE我们已经掌握其基本运行原理。下面我们主要介绍一下ICe的多端口邦定的问题。ICE使用TCP/IP 和UDP协议,以及基于TCP/IP之上的SSL协议。SSL协议可以充分保障Server...
  • neubuffer
  • neubuffer
  • 2013-11-23 14:27
  • 2269

ice eclipse安装和项目初建

1.安装ice 2.在eclipse中安装ice插件 在help->Eclipse Marketplace中搜索ice,并下载 如下图所示,如果出现Ice Builder表示安装成功,这时设置...
  • qq_31678877
  • qq_31678877
  • 2016-10-31 10:44
  • 1104

CodeForces 217A Ice Skating(并查集思路)

题目链接题目大概意思就是 在坐标系上 给你 几个点坐标 看他们需要几个中间点能让他们每两个都能连成一条线!这每个点就和 并查集的里每个点很像 看有几个祖先 祖先减一就是 这题答案! 所以 这题完全...
  • yexiaohhjk
  • yexiaohhjk
  • 2015-04-10 23:54
  • 540

并查集Codeforces Round #134 (Div. 1), problem: (A) Ice Skating

A. Ice Skating time limit per test 2 seconds memory limit per test 256 megabytes in...
  • caojiangxia
  • caojiangxia
  • 2015-05-11 09:40
  • 222

CF218C:Ice Skating(并查集)

C. Ice Skating time limit per test 2 seconds memory limit per test 256 megabytes in...
  • junior19
  • junior19
  • 2017-02-11 23:26
  • 102

codeforces 217A-- Ice Skating

Bajtek is learning to skate on ice. He’s a beginner, so his only mode of transportation is pushing o...
  • qingxi1512
  • qingxi1512
  • 2016-11-19 10:44
  • 99

Codeforces Round #134 (Div. 2)C. Ice Skating

并查集。 这么久没做题了,竟然快速的敲了个并查集出来还是蛮爽的。 这个题目我是把能够彼此到达(中间可以通过其他点)的那些点归纳到一个集合中,最后统计集合的数目,需要新加的点就是集合的数目减1。 ...
  • CodeSinger
  • CodeSinger
  • 2012-08-18 20:38
  • 1089

Codeforces---Ice Skating

题目链接: http://codeforces.com/contest/218/problem/C   Ice Skating Bajtek is learning t...
  • vip_wesley
  • vip_wesley
  • 2012-09-04 10:37
  • 205

Codeforces Round #134 (Div. 2) C. Ice Skating

C. Ice Skating time limit per test 2 seconds memory limit per test 256 megabytes in...
  • u011111528
  • u011111528
  • 2014-03-21 17:19
  • 439
    个人资料
    • 访问:42348次
    • 积分:1667
    • 等级:
    • 排名:千里之外
    • 原创:128篇
    • 转载:3篇
    • 译文:0篇
    • 评论:5条
    最新评论