题目描述

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. Docker学习之docker-compose

    docker-compose 安装 1.Mac/Windows: 安装docker的时候附带安装了. 2.Linux: curl https://github.com/docker/compose L ...

  2. [C++]类的设计(2)——拷贝控制(阻止拷贝)

    1.阻止拷贝的原因:对于某些类来说,拷贝构造函数和拷贝赋值运算符没有意义.举例:iostream类阻止了拷贝,以避免多个对象写入或者读取相同的IO缓冲.   2.阻止拷贝的方法有两个:新标准中可以将成 ...

  3. RabbitMQ的六种工作模式总结

    最近学习RabbitMQ的使用方式,记录下来,方便以后使用,也方便和大家共享,相互交流. RabbitMQ的六种工作模式: 1.Work queues2.Publish/subscribe3.Rout ...

  4. Python集训营45天—Day07 (面向对象编程进阶)

    目录 1. @property装饰器 2. 魔法方法 3. 类属性和实例属性 4.静态方法和类方法 5. 单继承和多继承 6. 多态 7. del 方法 序言:上个章节我们了解了面向对象的基础知识,这 ...

  5. MySQL中对字段内容为Null的处理

    使用如下指令,意思就是 select IFNULL(jxjy,0) AS jxjy from yourTable ifnull(a,b) 意思是指:如果字段a为null,就等于b if( sex = ...

  6. jenkins离线安装插件的方法(无法访问外网)

    最近项目要迁移环境,无法访问外网,因此jenkins的安装配置需要离线操作,在此记录 jenkins下载安装好之后,跳过插件的安装,新建用户进入jenkins界面,这些前置步骤我在之前的随笔里有写具体 ...

  7. Kerberoasting攻击

    前面我们介绍了<Windows本地认证>.<Windows网络认证>.<Windows域认证>和<SPN扫描>,这次继续讲解域内相关的东西. 0x01介 ...

  8. Django序列化&django REST framework

    第一章.Django序列化操作 1.django的view实现商品列表页(基于View类) # 通过json来序列化,但手写字典key代码量较大,容易出错:还有遇到时间,图片序列化会报错 from g ...

  9. 23种设计模式之适配器模式(Adapter Pattern)

    适配 即在不改变原有实现的基础上,将原先不兼容的接口转换为兼容的接口.例如:二转换为三箱插头,将高电压转换为低电压等. 动机(Motivate):    在软件系统中,由于应用环境的变化,常常需要将“ ...

  10. 阿里云服务器CentOS6.9 tomcat配置https安全访问

    应用场景 上线微信小程序的时候,域名要求https安全格式,否则获取数据异常. 第一步.SSL证书获取 获取SSL证书方式很多种,包括网页生成.工具生成等,这里我使用阿里云平台获取免费ssl证书的方法 ...