题目描述:

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;
}

Codeforces K. Ice Skating(求强连通分量)的更多相关文章

  1. UESTC 901 方老师抢银行 --Tarjan求强连通分量

    思路:如果出现了一个强连通分量,那么走到这个点时一定会在强连通分量里的点全部走一遍,这样才能更大.所以我们首先用Tarjan跑一遍求出所有强连通分量,然后将强连通分量缩成点(用到栈)然后就变成了一个D ...

  2. poj 2186 tarjan求强连通分量

    蕾姐讲过的例题..玩了两天后才想起来做 貌似省赛之后确实变得好懒了...再努力两天就可以去北京玩了! 顺便借这个题记录一下求强连通分量的算法 1 只需要一次dfs 依靠stack来实现的tarjan算 ...

  3. [Uva247][Tarjan求强连通分量][Calling Circles]

    题目大意: 例如:A跟B打电话,B跟C打电话,C跟A打电话..D跟E打电话,E跟D不打电话.则A,B,C属于同一个电话圈,D,E分别属于一个电话圈,问有多少个电话圈. 分析 就是裸的求强连通分量,直接 ...

  4. HDU 1827 Summer Holiday(tarjan求强连通分量+缩点构成新图+统计入度+一点贪心思)经典缩点入门题

    Summer Holiday Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  5. CCF 高速公路 tarjan求强连通分量

    问题描述 某国有n个城市,为了使得城市间的交通更便利,该国国王打算在城市之间修一些高速公路,由于经费限制,国王打算第一阶段先在部分城市之间修一些单向的高速公路. 现在,大臣们帮国王拟了一个修高速公路的 ...

  6. kosaraju算法求强连通分量

    什么是强连通分量?在这之前先定义一个强连通性(strong connectivity)的概念:有向图中,如果一个顶点s到t有一条路径,t到s也有一条路径,即s与t互相可达,那么我们说s与t是强连通的. ...

  7. UVALive 4262——Trip Planning——————【Tarjan 求强连通分量个数】

    Road Networks Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Stat ...

  8. Tarjan 算法求 LCA / Tarjan 算法求强连通分量

    [时光蒸汽喵带你做专题]最近公共祖先 LCA (Lowest Common Ancestors)_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili tarjan LCA - YouTube Tarj ...

  9. Tarjan模板——求强连通分量

    Tarjan求强连通分量的流程在这个博客讲的很清楚,再加上我也没理解透,这里就不写了. 缩点:将同一个连通块内的点视为同一个点. 扔一道模板题:codeVS2822爱在心中 第一问很显然就是求点数大于 ...

随机推荐

  1. python总结六

    1.python中主要存在四种命名方式: object #公用方法 _object #半保护                  #被看作是“protect”,意思是只有类对象和子类对象自己能访问到这些 ...

  2. js 校验手机号码格式

      手机号码格式简单校验 原理:判断手机号是否以已经发行的手机号码段开头,而且判断其余9位是否是数字. 方式一: var phone = $('#phone').val(); var regex = ...

  3. Java 并发系列之五:java 锁

    1. Lock接口 2. 队列同步器AQS 3. 重入锁 ReentrantLock 4. 读写锁 ReentrantReadWriteLock 5. LockSupport工具 6. Conditi ...

  4. 使用IDEA创建一个Servlet应用程序

    使用IDEA创建一个Servlet应用程序 第一步:创建web应用 选择web application应用,之后填写项目名称等. 第二步:项目配置 在WEB-INF目录下创建两个文件夹:classes ...

  5. ValueError: Graph disconnected: cannot obtain value for tensor Tensor

    一般是Input和下面的变量重名了,导致model里面的input变成了第二次出现的Input变量,而不是最开始模型中作为输入的Input变量 改正方法:给第二个变量赋一个新名字即可

  6. 【Java】15分钟快速体验阿里Java诊断工具Arthas

    [墙裂推荐]15分钟快速体验阿里Java诊断工具Arthas : https://alibaba.github.io/arthas/arthas-tutorials?language=cn&i ...

  7. 【C/C++开发】C++11 并发指南三(std::mutex 详解)

    本系列文章主要介绍 C++11 并发编程,计划分为 9 章介绍 C++11 的并发和多线程编程,分别如下: C++11 并发指南一(C++11 多线程初探)(本章计划 1-2 篇,已完成 1 篇) C ...

  8. 《Linux就该这么学》培训笔记_ch03_管道符、重定向与环境变量

    <Linux就该这么学>培训笔记_ch03_管道符.重定向与环境变量 文章最后会post上书本的笔记照片. 文章主要内容: 输入输出重定向 管道命令符 命令行的通配符 常用的转义字符 重要 ...

  9. python的值传递与引用传递

    首先还是应该科普下函数参数传递机制,传值和传引用是什么意思? 函数参数传递机制问题在本质上是调用函数(过程)和被调用函数(过程)在调用发生时进行通信的方法问题.基本的参数传递机制有两种:值传递和引用传 ...

  10. Docker 一步搞定 ZooKeeper 集群的搭建

    Docker 一步搞定 ZooKeeper 集群的搭建 背景 原来学习 ZK 时, 我是在本地搭建的伪集群, 虽然说使用起来没有什么问题, 但是总感觉部署起来有点麻烦. 刚好我发现了 ZK 已经有了 ...