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. underscore javascript工具库支持seajs模块化

    underscore是一个很有用的js工具库,但是好像默认不支持seajs模块化 新建一个文件例如叫做xx.js 谈后,键入 define(function(require,exports,modul ...

  2. [Matlab] fprintf

    %s format as a string%d format with no fractional part (integer format)%f format as a oating-point v ...

  3. 使用openssl在命令行加密

    对于需要在应用软件中进行加密编程的开发者,通过命令行把基本的加密操作做一遍是很有意义的.openssl支持在命令行进行各种基本加密算法的操作.这些操作过程无需编程,其命令参数与程序函数调用加密的参数有 ...

  4. 上下文(Context)和作用域(Scope)

    函数的每次调用都有与之紧密相关的作用域和上下文.从根本上来说,作用域是基于函数的,而上下文是基于对象的. 换句话说,作用域涉及到所被调用函数中的变量访问,并且不同的调用场景是不一样的.上下文始终是th ...

  5. 08.StreamReader和StreamWrite的学习

    StreamReader和StreamWrite是用来操作字符的 namespace _21.对StreamReader和StreamWriter的学习 { class Program { stati ...

  6. 关闭Windows 系统当前连接的Wifi以及判断物理\虚拟网卡,有线\无线网卡

    1.关闭wifi ,调用Api [DllImport("Wlanapi.dll", SetLastError = true)] public static extern uint ...

  7. Linux 连接 Xshell 及网络配置

    一.准备工具 在WMware上已经装有Linux系统:WMware安装CentOS7文章. xshell连接工具: 二.修改相关配置 切换到root用户下: 配置主机名(可选): #方法一:替换原主机 ...

  8. [SQL SERVER系列]工作经常使用的SQL整理,实战篇(二)[原创]

    工作经常使用的SQL整理,实战篇,地址一览: 工作经常使用的SQL整理,实战篇(一) 工作经常使用的SQL整理,实战篇(二) 工作经常使用的SQL整理,实战篇(三) 接着上一篇“工作经常使用的SQL整 ...

  9. JS中绑定事件顺序(事件冒泡与事件捕获区别)

    在JS中,绑定的事件默认的执行时间是在冒泡阶段执行,而非在捕获阶段(重要),这也是为什么当父类和子类都绑定了某个事件,会先调用子类绑定的事件,后调用父类的事件.直接看下面实例 <!Doctype ...

  10. Python contextlib.contextmanager

    看着代码又发现了一个奇怪的东西: @contextlib.contextmanager def __call__(self, incoming): result_wrapper = [] yield ...