luogu P3420 [POI2005]SKA-Piggy Banks
题目描述
Byteazar the Dragon has NN 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.
有n个储钱罐,每个的钥匙都在另一个里面,求取出所有储钱罐中的钱最少要砸开几个
输入输出格式
输入格式:
The first line of the standard input contains a single integer NN (1\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 11 to NN. Next, there are NNlines: the (i+1)(i+1)'st line contains a single integer - the number of the piggy bank in which the ii'th key has been placed.
输出格式:
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.
输入输出样例
4
2
1
2
4
2 并查集,由于每个盒子的入度只能是1,既然能并到一起,那么一定有一个起始节点;
最后统计集合个数
#include<cstdio>
int n;
const int maxn = ;
int a[maxn];
int father[maxn];
int find(int x) {
if(father[x]!=x)father[x]=find(father[x]);
return father[x];
}
void unionn(int x,int y) {
int fx=find(x),fy=find(y);
if(fx!=fy)father[fx]=fy;
}
int main () {
scanf("%d",&n);
for(int i=;i<=n;++i)father[i]=i;
for(int i=;i<=n;++i) {
scanf("%d",a+i);unionn(i,a[i]);
}
int ans=;
for(int i=;i<=n;++i)
if(father[i]==i)ans++;
printf("%d\n",ans);
return ;
}
luogu P3420 [POI2005]SKA-Piggy Banks的更多相关文章
- [BZOJ1529][POI2005]ska Piggy banks
[BZOJ1529][POI2005]ska Piggy banks 试题描述 Byteazar 有 N 个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. Byteazar 已经把每个存钱罐的钥匙放 ...
- BZOJ 1529: [POI2005]ska Piggy banks( 并查集 )
每一连通块砸开一个就可以拿到所有的钱, 所以用并查集求连通块数 ------------------------------------------------------------------- ...
- 【BZOJ1529】[POI2005]ska Piggy banks Tarjan
[BZOJ1529][POI2005]ska Piggy banks Description Byteazar 有 N 个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. Byteazar 已经把每个 ...
- bzoj1529 [POI2005]ska Piggy banks 并查集
[POI2005]ska Piggy banks Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1508 Solved: 763[Submit][Sta ...
- Taran 缩点【bzoj1529】[POI2005]ska Piggy banks
[bzoj1529][POI2005]ska Piggy banks Description Byteazar 有 N 个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. Byteazar 已经把每个 ...
- 【BZOJ】1529 [POI2005]ska Piggy banks
[算法](强连通分量)并查集 [题解] 1.用tarjan计算强连通分量并缩点,在新图中找入度为0的点的个数就是答案. 但是,会爆内存(题目内存限制64MB). 2.用并查集,最后从1到n统计fa[i ...
- BZOJ 1529 [POI2005]ska Piggy banks(并查集)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1529 [题目大意] 给出一张n个点n条边的有向图,问选取几个点为起点可以遍历全图 [题 ...
- BZOJ 1529 [POI2005]ska Piggy banks:并查集
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1529 题意: Byteazar有N个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. By ...
- 【BZOJ】【1529】 【POI2005】ska Piggy banks
本来以为是tarjan缩点……但是64MB的空间根本不足以存下原图和缩点后的新图.所以呢……并查集= = orz hzwer MLE的tarjan: /************************ ...
随机推荐
- Python学习笔记3(字典)
创建字典 dict函数 字典的格式化字符串 字典方法 clear copy fromkeys 序列是一个按照一定顺序将值进行组织的数据结构形式,可以通过索引对其进行征引.另外还有一种数据结构是通过名字 ...
- 【树状数组 离散化】bzoj1573: [Usaco2009 Open]牛绣花cowemb
解方程题! Description Bessie学会了刺绣这种精细的工作.牛们在一片半径为d(1 <= d <= 50000)的圆形布上绣花. 它们一共绣了N (2 <= N < ...
- docker系列之分区挂载和数据卷
容器中的文件系统是独立的, 一旦容器被删除, 则文件系统也会被删除. 如果想容器和实体机在文件系统层面打通, 可以把指定目录挂载到容器当中: docker run -d -p 5000:22 -v / ...
- JavaScript 基础:Babel 转译 class 过程窥探
零.前言 虽然在 JavaScript 中对象无处不在,但这门语言并不使用经典的基于类的继承方式,而是依赖原型,至少在 ES6 之前是这样的.当时,假设我们要定义一个可以设置 id 与坐标的类,我们会 ...
- input标签内容改变触发的事件
原生方法 onchange事件 <input type="text" onchange="onc(this)"> function onc(data ...
- 条款52:写了placement new 也要写placement delete(write placement delete if you write placement new)
NOTE: 1.当你写一个placement operator new .请确定也要写出了对应的placement operator delete.如果没有这样做,你的程序可能发生隐晦而时断时续的内存 ...
- PAT Basic 1030
1030 完美数列 给定一个正整数数列,和正整数p,设这个数列中的最大值是M,最小值是m,如果M <= m * p,则称这个数列是完美数列. 现在给定参数p和一些正整数,请你从中选择尽可能多的数 ...
- svg path 动画效果
http://www.zhangxinxu.com/wordpress/2014/04/animateion-line-drawing-svg-path-%E5%8A%A8%E7%94%BB-%E8% ...
- unittest跳过测试和预期失败
在运行测试时,有时需要直接跳过某些测试用例,或者当用例符合某个条件时跳过测试,又或者直接将测试用例设置为失败.unittest提供了这些需求的装饰器. unittest.skip(reason) 无条 ...
- Codeforces Round #412 Div. 2 补题 D. Dynamic Problem Scoring
D. Dynamic Problem Scoring time limit per test 2 seconds memory limit per test 256 megabytes input s ...