PAT1067. Sort with Swap(0, *) (25) 并查集

题目大意

给定一个序列, 只能进行一种操作: 任一元素与 0 交换位置, 问最少需要多少次交换.

思路

最优解就是每次 0 都和所在位置本应在的元素交换位置, 共 n - 1 次, 但是在交换中 0 可能会被交换到 0 号位置.

仔细思考一下, 其实每次换到 0 就是一个图的连通分量, 按照输入的次序和输入的值, 可以求出图共有多少个联通分量.

每个联通分量若要换回去, 需要 n - 1 次交换, 但是只能用 0 交换, 所以不含 0 且 结点个数不为 1 的连通分量需要进行 加 2 操作 (把 0 换进来, 排序, 再把 0 换出去).

代码

#include <cstdio>
#include <cstring>
#define MAXN 100100
using namespace std;
int arr[MAXN];
int cnt[MAXN];
int getFather(int a){
if(a == arr[a])
return a;
return arr[a] = getFather(arr[a]);
}
void merge(int a, int b){
a = getFather(a);
b = getFather(b);
if(a < b)
arr[b] = a;
else
arr[a] = b;
}
int main(){
int nNum;
scanf("%d", &nNum);
for(int i = 0; i < nNum; arr[i] = i++);
for(int i = 0; i < nNum; i++)
arr[i] = i; for(int i = 0; i < nNum; i++){
int val;
scanf("%d", &val);
merge(i, val);
} for(int i = 0; i < nNum; i++){
cnt[arr[i]]++;
} int sum = 0;
for(int i = 0; i < nNum; i++){
if(arr[i] != i)
continue;
sum += cnt[i] - 1;
if(i != 0 && cnt[i] != 1)
sum += 2;
}
printf("%d", sum);
return 0;
}

PAT1067. Sort with Swap(0, *) (25) 并查集的更多相关文章

  1. pat1067. Sort with Swap(0,*) (25)

    1067. Sort with Swap(0,*) (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...

  2. Pat1067:Sort with Swap(0,*)

    1067. Sort with Swap(0,*) (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...

  3. 1067. Sort with Swap(0,*) (25)【贪心】——PAT (Advanced Level) Practise

    题目信息 1067. Sort with Swap(0,*) (25) 时间限制150 ms 内存限制65536 kB 代码长度限制16000 B Given any permutation of t ...

  4. 1067. Sort with Swap(0,*) (25)

    时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given any permutation of the num ...

  5. PAT Advanced 1067 Sort with Swap(0,*) (25) [贪⼼算法]

    题目 Given any permutation of the numbers {0, 1, 2,-, N-1}, it is easy to sort them in increasing orde ...

  6. PAT (Advanced Level) 1067. Sort with Swap(0,*) (25)

    只对没有归位的数进行交换. 分两种情况: 如果0在最前面,那么随便拿一个没有归位的数和0交换位置. 如果0不在最前面,那么必然可以归位一个数字,将那个数字归位. 这样模拟一下即可. #include& ...

  7. PAT甲题题解-1067. Sort with Swap(0,*) (25)-贪心算法

    贪心算法 次数最少的方法,即:1.每次都将0与应该放置在0位置的数字交换即可.2.如果0处在自己位置上,那么随便与一个不处在自己位置上的数交换,重复上一步即可.拿样例举例:   0 1 2 3 4 5 ...

  8. PAT 1067. Sort with Swap(0,*)

    1067. Sort with Swap(0,*) (25)   Given any permutation of the numbers {0, 1, 2,..., N-1}, it is easy ...

  9. PAT 甲级 1067 Sort with Swap(0, i) (25 分)(贪心,思维题)*

    1067 Sort with Swap(0, i) (25 分)   Given any permutation of the numbers {0, 1, 2,..., N−1}, it is ea ...

随机推荐

  1. 使用params

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. django管理界面使用与bootstrap模板使用

    一.bootstrap模板使用 1.去bootstrap官网找一个合适的模板,下载下来,右键另存为即可 bootstrap官网---->bootstrap中文文档3-------->起步- ...

  3. C#自定义控件 在 Toolbox显示不了的问题

    1) Close your solution2) Tools->Options->"Windows Form Designer" - find AutoToolboxP ...

  4. 选择排序——Java实现

    一.排序思想 选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理是: 从待排序列中选出最小(或最大)的一个元素,记录其下标(数组)的位置: 将记录的下标值与待排序列的第一个 ...

  5. Java入门到精通——调错篇之EasyUI+SpringMVC Form表单提交到Contorller中文字出现乱码

    一.错误现象. 界面Post提交到Contorller的时候在Contorller中出现乱码. 二.问题解决. 在Web.xml下加入以下代码就可以解决. <filter> <fil ...

  6. Android 隐式 Intent 跳转注意事项

    前几天正好看到<阿里巴巴 Android 开发手册>中提到的: “Activity 间通过隐式 Intent 的跳转,在发出 Intent 之前必须通过 resolveActivity 检 ...

  7. ng-repeat和ng-options区别

    ng-repeat ="x in XXX" ng-options="x.*** for x in XXX“ ng-repeat 写法 <select> < ...

  8. spring security基于数据库表进行认证

    我们从研究org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl.class的源码开始 public class JdbcDaoI ...

  9. 使用auto_ptr需要注意的事项

    注:C++11 已不推荐使用,应使用scoped_ptr/shared_ptr. 部分原因就是如下的注意事项. 转自:http://patmusing.blog.163.com/blog/static ...

  10. C/C++:函数的调用约定(Calling Convention)和名称修饰(Decorated Name)以及两者不匹配引起的问题

    转自:http://blog.csdn.net/zskof/article/details/3475182 注:C++有着与C不同的名称修饰,主要是为了解决重载(overload):调用约定则影响函数 ...