题目描述

Byteazar the Dragon has NNN piggy banks. Each piggy bank can either be opened with its corresponding key or smashed. Byteazar has put the keys in some of the piggy banks - he remembers which key has been placed in which piggy bank. Byteazar intends to buy a car and needs to gain access to all of the piggy banks. However, he wants to destroy as few of them as possible. Help Byteazar to determine how many piggy banks have to be smashed.

TaskWrite a programme which:

reads from the standard input the number of piggy banks and the deployment of their corresponding keys,finds the minimal number of piggy banks to be smashed in order to gain access to all of them,writes the outcome to the standard output.

Byteazar the Dragon拥有N个小猪存钱罐。每一个存钱罐能够用相应的钥匙打开或者被砸开。Byteazar已经将钥匙放入到一些存钱罐中。现在已知每个钥匙所在的存钱罐,Byteazar想要买一辆小汽车,而且需要打开所有的存钱罐。然而,他想要破坏尽量少的存钱罐,帮助Byteazar去决策最少要破坏多少存钱罐。

任务:

写一段程序包括:

读入存钱罐的数量以及相应的钥匙的位置,求出能打开所有存钱罐的情况下,需要破坏的存钱罐的最少数量并将其输出。

输入输出格式

输入格式:

The first line of the standard input contains a single integer NNN ( 1≤N≤1 000 0001\le N\le 1\ 000\ 0001≤N≤1 000 000
) - this is the number of piggy banks owned by the dragon. The piggy
banks (as well as their corresponding keys) are numbered from 111 to NNN . Next, there are NNN lines: the (i+1)(i+1)(i+1) 'st line contains a single integer - the number of the piggy bank in which the iii 'th key has been placed.

第一行:包括一个整数N(1<=N<=1000000),这是Byteazar the Dragon拥有的存钱罐的数量。

存钱罐(包括它们对应的钥匙)从1到N编号。

接下来有N行:第i+1行包括一个整数x,表示第i个存钱罐对应的钥匙放置在了第x个存钱罐中。

输出格式:

The first and only line of the standard output should contain a
single integer - the minimal number of piggy banks to be smashed in
order to gain access to all of the piggy banks.

仅一行:包括一个整数,表示能打开所有存钱罐的情况下,需要破坏的存钱罐的最少数量。

输入输出样例

输入样例#1:

4
2
1
2
4
输出样例#1:

2


用并查集维护连通性,然后最后统计有多少个树根不一样的。


#include <iostream>
#include <cstdio>
using namespace std;
inline int read(){
int res=;char ch=getchar();
while(!isdigit(ch)) ch=getchar();
while(isdigit(ch)){res=(res<<)+(res<<)+(ch^);ch=getchar();}
return res;
}
int n;
int fa[];
int Find(int x){return x==fa[x]?x:fa[x]=Find(fa[x]);}
bool vis[];
int ans; int main()
{
n = read();
for (int i = ; i <= n ; i ++) fa[i] = i;
for (int i = ; i <= n ; i ++)
{
int x = read();
int fx = Find(x), fy = Find(i);
if (fx != fy)
{
fa[fx] = fy;
}
}
for (int i = ; i <= n ; i ++)
{
int x = Find(i);
if (!vis[x]) ans++;
vis[x]=;
}
return printf("%d\n", ans), ;
}

[Luogu3420][POI2005]SKA-Piggy Banks的更多相关文章

  1. [BZOJ1529][POI2005]ska Piggy banks

    [BZOJ1529][POI2005]ska Piggy banks 试题描述 Byteazar 有 N 个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. Byteazar 已经把每个存钱罐的钥匙放 ...

  2. BZOJ 1529: [POI2005]ska Piggy banks( 并查集 )

    每一连通块砸开一个就可以拿到所有的钱, 所以用并查集求连通块数 ------------------------------------------------------------------- ...

  3. 【BZOJ1529】[POI2005]ska Piggy banks Tarjan

    [BZOJ1529][POI2005]ska Piggy banks Description Byteazar 有 N 个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. Byteazar 已经把每个 ...

  4. bzoj1529 [POI2005]ska Piggy banks 并查集

    [POI2005]ska Piggy banks Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1508  Solved: 763[Submit][Sta ...

  5. Taran 缩点【bzoj1529】[POI2005]ska Piggy banks

    [bzoj1529][POI2005]ska Piggy banks Description Byteazar 有 N 个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. Byteazar 已经把每个 ...

  6. 【BZOJ】1529 [POI2005]ska Piggy banks

    [算法](强连通分量)并查集 [题解] 1.用tarjan计算强连通分量并缩点,在新图中找入度为0的点的个数就是答案. 但是,会爆内存(题目内存限制64MB). 2.用并查集,最后从1到n统计fa[i ...

  7. BZOJ 1529 [POI2005]ska Piggy banks(并查集)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1529 [题目大意] 给出一张n个点n条边的有向图,问选取几个点为起点可以遍历全图 [题 ...

  8. BZOJ 1529 [POI2005]ska Piggy banks:并查集

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1529 题意: Byteazar有N个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. By ...

  9. 【BZOJ】【1529】 【POI2005】ska Piggy banks

    本来以为是tarjan缩点……但是64MB的空间根本不足以存下原图和缩点后的新图.所以呢……并查集= = orz hzwer MLE的tarjan: /************************ ...

随机推荐

  1. Git客户端下载

    链接:http://pan.baidu.com/s/1eRXsITO 密码:4i6e

  2. 无法解析的外部符号,该符号在xxx函数中被引用

    无法解析的外部符号.........,该符号在函数.........被引用 在我们敲代码的过程中,我们偶尔会遇到这个问题,这个问题大多数都是因为你自己的程序有问题,而不是缺少相应的库文件.话不多说,直 ...

  3. 01 (H5*) Vue第一天

    目录 1:什么是Vue.js 2:MVC和MVVM. 3:为什么要学习前段框架 4:框架和库的区别 5:怎么使用Vue. 6:常见的Vue指令 7:  五大事件修饰符 8:在vue中使用class样式 ...

  4. 1、Spark 2.1 源码编译支持CDH

    目前CDH支持的spark版本都是1.x, 如果想要使用spark 2x的版本, 只能编译spark源码生成支持CDH的版本. 一.准备工作 找一台Linux主机, 由于spark源码编译会下载很多的 ...

  5. Python学习-is和==区别, encode和decode

    一.is 和 == 介绍 1. is  比较的是两个对象的内存地址是否相同,它们是不是同一个对象. 2. ==  比较的是两个对象的内容是否相同. 在使用is前,先介绍Python的一个内置函数id( ...

  6. 代码审计之XSS及修复

    xss在平时的测试中,还是比较重要的,如果存在储存型xss,就可以做很多事情了,打cookie,添加管理员等等很多操作. 以下所有代码都是我自己写的,可能有不美观,代码错误等等问题,希望大家可以指正. ...

  7. chsime.exe cpu占用高

    打开管理员的命令提示符,运行 if exist "%SystemRoot%\System32\InputMethod\CHS\ChsIME.exe" (takeown /f &qu ...

  8. Linux版本号的数值含义

    Linux内核版本有两种:稳定版和开发版 ,Linux内核版本号由3组数字组成:第一个组数字.第二组数字.第三组数字.第一个组数字:目前发布的内核主版本.第二个组数字:偶数表示稳定版本:奇数表示开发中 ...

  9. Java之字符流读写文件、文件的拷贝

    字符流读数据 – 按单个字符读取 创建字符流读文件对象: Reader reader = new FileReader("readme.txt"); 调用方法读取数据: int d ...

  10. JS/Jquery 表单方式提交总结

    1. submit提交 (1). submit 按钮式提交 缺点:在提交前不可修改提交的form表单数据 // 1. html <form method="post" act ...