概述
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
Copy
2
2 1
1 2
output
Copy
1
input
Copy
2
2 1
4 1
output
Copy
0
这个题目一直理解错题意了,一直认为是求n个点的最短路。题目要求的是让n个点符合条件,需要添加的点是多少。
题解:如果两个点x或者y相同,这两个点就“连通”,即符合题目要求,转化一下,是并查集模板题。也可以用dfs求连通块。
并查集
AC Code:
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<bitset>
#include<cctype>
#define LL long long
#define maxn (LL)1e5
#define INF 0x3f3f3f3f
const LL eps = 0.00001;
using namespace std;
struct node
{
int x,y;
};
int pre[ 105];
int Find(int x)
{
return pre[x]==x?x:pre[x]=Find(pre[x]);
}
void join(int x,int y)
{
int q, p;
q=Find(x);
p=Find(y);
if(q!=p)
{
pre[q] = p;
}
}
void init()
{
for(int i = 1;i<=100;++i)
pre[i] = i;
}
node ans[102];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
#endif // ONLINE_JUDG
int n;
cin>>n;
init();
for(int i = 1;i<=n;++i)
cin>>ans[i].x>>ans[i].y;
for(int i = 1;i<n;++i)
{
for(int j = i+1;j<=n;++j)
{
if(ans[i].x==ans[j].x||ans[i].y==ans[j].y) join(i,j);
}
}
int sum = 0;
for(int i = 1;i<=n;++i)
for(int j = i+1;j<=n;++j)
{
int q = Find(i);
int p = Find(j);
if(q!=p) {
sum++;
join(q,p);
}
}
cout<<sum;
}
dfs
AC Code:
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<bitset>
#include<cctype>
#define LL long long
#define maxn (LL)1e5
#define INF 0x3f3f3f3f
const LL eps = 0.00001;
using namespace std;
struct node{
int x,y;
};
node ans[102];
bool vis[102];
int n;
void dfs(int idx)
{
vis[idx] =1;
for(int i = 1;i<=n;++i)
{
if((ans[i].x==ans[idx].x||ans[i].y==ans[idx].y)&&vis[i]==0) dfs(i);
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
#endif // ONLINE_JUDE
cin>>n;
for(int i=1;i<=n;++i)
{
cin>>ans[i].x>>ans[i].y;
}
int sum =0;
for(int i = 1;i<=n;++i)
{
if(vis[i]) continue;
dfs(i);
sum++;
}
cout<<sum-1;
}
最后
以上就是明理手套为你收集整理的C. Ice Skating(并查集 | DFS)的全部内容,希望文章能够帮你解决C. Ice Skating(并查集 | DFS)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复