题目描述:

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 x**i and y**i (1 ≤ x**i, y**i ≤ 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

Copy

2
2 1
1 2

Output

Copy

1

Input

Copy

2
2 1
4 1

Output

Copy

0

思路:

题目是说给一些点,这些点只有在x坐标相同或者y坐标相同才能相互连接,现在为了让这些点能够全部连通问需要再加几个点。这题一开始可能会觉得有点麻烦,因为这是好多个点,我怎么知道那些点能够相互连通那些点不能,加点又要加在什么地方。但其实不用考虑加点在什么地方,我们只需要关注哪些点是连通的就行了。把这些点建成一个图,可以连通的建一条边,然后就是图上的强连通分量的数目了。一个强连通分量内点是可以相互到达的,有n个强连通分量我们就把强连通分量再连起来这些点就组成了一个强连通分量。怎么连,只要再建n-1条边就行了。这n-1条边实际上就对应的是加建的点。然而我忘了tarjan怎么写,照抄书上的另一种算法,但好像那个模板有点问题,于是看了tarjan再写就对了。

代码:

#include <iostream>
#include <stack>
#include <vector>
#define max_n 1005
using namespace std;
typedef pair<int,int> PII;
vector<PII> vec;
int head[max_n];
int cnt = 0;
struct edge
{
int v;
int nxt;
}e[max_n<<1];
void add(int u,int v)
{
++cnt;
e[cnt].v = v;
e[cnt].nxt = head[u];
head[u] = cnt;
}
int n,m;
int idx = 0;
int Bcnt;
int instack[max_n];
int dfn[max_n];
int low[max_n];
int belong[max_n];
stack<int> s;
void tarjan(int u)
{
dfn[u] = low[u] = ++idx;
s.push(u);
instack[u]=1;
int v;
for(int i = head[u];i;i=e[i].nxt)
{
v = e[i].v;
if(!dfn[v])
{
tarjan(v);
low[u] = min(low[u],low[v]);
}
else if(instack[v])
{
low[u] = min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u])
{
Bcnt++;
do
{
v=s.top();
s.pop();
instack[v]=0;
belong[v]=Bcnt;
}while(u!=v);
}
}
int main()
{
cin >> n;
for(int i = 0;i<n;i++)
{
int x,y;
cin >> x >> y;
vec.push_back(PII(x,y));
}
for(int i = 0;i<n;i++)
{
for(int j = i+1;j<n;j++)
{
if(vec[i].first==vec[j].first||vec[i].second==vec[j].second)
{
add(i,j);
add(j,i);
}
}
}
for(int i = 0;i<n;i++)
{
if(!dfn[i])
{
tarjan(i);
}
}
cout << Bcnt-1 << endl;
return 0;
}

最新文章

  1. Java接口与实例化
  2. Java后端开发
  3. jsp %EF%BB%BF
  4. 误用ArrayListMultimap引发的问题
  5. 跌跌撞撞,用MAVEN看图说话的编译了一个JAR出来
  6. 清理ms sql server 大日志文件数据
  7. 2.3 DHC REST
  8. C++ 11 学习1:类型自动推导 auto和decltype
  9. webdriver API中文文档
  10. Vuejs——v-on
  11. Sublime Text3 远程 Linux
  12. DNS的域名的解析解决办法(openDNS)
  13. 11G新特性 -- OLTP Table Compression
  14. 原生js上传图片时的预览
  15. Android开发教程 - 使用Data Binding(八)使用自定义Interface
  16. TextView中文文档
  17. MySQL配置主主及主从备份
  18. “全栈2019”Java异常第十章:throw与throws区别详解
  19. 08_java超市管理系统
  20. upsampling(上采样)&amp; downsampled(降采样)

热门文章

  1. ##xcode 文件模板自定义
  2. java web开发入门四(spring)基于intellig idea
  3. zookeeper shell
  4. [Gamma] 发布说明
  5. React组件介绍与使用(父传子、子传父、兄弟传)
  6. Alpha冲刺(8/10)——2019.4.30
  7. Haskell-chp01
  8. idea 添加默认注释
  9. .net(2)
  10. Java面试宝典(2020版)