Codeforces K. Ice Skating(求强连通分量)
题目描述:
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(求强连通分量)的更多相关文章
- UESTC 901 方老师抢银行 --Tarjan求强连通分量
思路:如果出现了一个强连通分量,那么走到这个点时一定会在强连通分量里的点全部走一遍,这样才能更大.所以我们首先用Tarjan跑一遍求出所有强连通分量,然后将强连通分量缩成点(用到栈)然后就变成了一个D ...
- poj 2186 tarjan求强连通分量
蕾姐讲过的例题..玩了两天后才想起来做 貌似省赛之后确实变得好懒了...再努力两天就可以去北京玩了! 顺便借这个题记录一下求强连通分量的算法 1 只需要一次dfs 依靠stack来实现的tarjan算 ...
- [Uva247][Tarjan求强连通分量][Calling Circles]
题目大意: 例如:A跟B打电话,B跟C打电话,C跟A打电话..D跟E打电话,E跟D不打电话.则A,B,C属于同一个电话圈,D,E分别属于一个电话圈,问有多少个电话圈. 分析 就是裸的求强连通分量,直接 ...
- HDU 1827 Summer Holiday(tarjan求强连通分量+缩点构成新图+统计入度+一点贪心思)经典缩点入门题
Summer Holiday Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- CCF 高速公路 tarjan求强连通分量
问题描述 某国有n个城市,为了使得城市间的交通更便利,该国国王打算在城市之间修一些高速公路,由于经费限制,国王打算第一阶段先在部分城市之间修一些单向的高速公路. 现在,大臣们帮国王拟了一个修高速公路的 ...
- kosaraju算法求强连通分量
什么是强连通分量?在这之前先定义一个强连通性(strong connectivity)的概念:有向图中,如果一个顶点s到t有一条路径,t到s也有一条路径,即s与t互相可达,那么我们说s与t是强连通的. ...
- UVALive 4262——Trip Planning——————【Tarjan 求强连通分量个数】
Road Networks Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Stat ...
- Tarjan 算法求 LCA / Tarjan 算法求强连通分量
[时光蒸汽喵带你做专题]最近公共祖先 LCA (Lowest Common Ancestors)_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili tarjan LCA - YouTube Tarj ...
- Tarjan模板——求强连通分量
Tarjan求强连通分量的流程在这个博客讲的很清楚,再加上我也没理解透,这里就不写了. 缩点:将同一个连通块内的点视为同一个点. 扔一道模板题:codeVS2822爱在心中 第一问很显然就是求点数大于 ...
随机推荐
- Linux笔记本合上屏幕不待机
Linux笔记本合上屏幕不待机[]# vim /etc/systemd/logind.conf# This file is part of systemd.## systemd is free sof ...
- [技术博客] 微信小程序的formid获取
微信小程序的formid获取 formId的触发 微信小程序可以通过收集用户的formid,获取formid给用户主动推送微信消息.获取formid有两个途径,一个是触发一次表单提交,或者触发一次支付 ...
- 【Docker】涨姿势,深入了解Dockerfile 中的 COPY 与 ADD 命令
参考资料:https://www.cnblogs.com/sparkdev/p/9573248.html Dockerfile 中提供了两个非常相似的命令 COPY 和 ADD,本文尝试解释这两个命令 ...
- Elasticsearch详解-续
Elasticsearch详解-续 Chandler_珏瑜 关注 7.6 2019.05.22 10:46* 字数 8366 阅读 675评论 4喜欢 25 5.3 性能调优 Elasticse ...
- nginx负载均衡原理
负载均衡在服务端开发中算是一个比较重要的特性.因为Nginx除了作为常规的Web服务器外,还会被大规模的用于反向代理前端,因为Nginx的异步框架可以处理很大的并发请求,把这些并发请求hold住之后就 ...
- List Map Set的线程安全
常见的ArrayList LinkedList HashMap TreeMap LinkedHashMap HashSet TreeSet LinkedHashSet 都是线程不安全的.如果要使用 ...
- AQS4源码
@SuppressWarnings("restriction") public abstract class AbstractQueuedSynchronizer1 extends ...
- [转帖]10分钟看懂Docker和K8S
10分钟看懂Docker和K8S https://zhuanlan.zhihu.com/p/53260098 2010年,几个搞IT的年轻人,在美国旧金山成立了一家名叫“dotCloud”的公司. 这 ...
- 小白的C++之路——求质数
初学C++,打算用博客记录学习的足迹.写了两个求质数的程序,修修改改. #include <iostream> #include <math.h> using namespac ...
- scala 函数式编程之集合操作
Scala的集合体系结构 // Scala中的集合体系主要包括:Iterable.Seq.Set.Map.其中Iterable是所有集合trait的根trai.这个结构与Java的集合体系非常相似. ...