Sicily 1350. Piggy banks
来源:互联网 发布:手机淘宝卖家版登陆 编辑:程序博客网 时间:2024/05/21 09:58
1350. Piggy banks
Constraints
Time Limit: 3 secs, Memory Limit: 32 MB
Description
Byteazar the Dragon has N 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.
Task
Write 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.
Input
注意:输入包含多个测试数据。
The first line of the standard input contains a single integer N (1 <= 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 1 to N. Next, there are N lines: the i+1st line contains a single integer - the number of the piggy bank in which the ith key has been placed.
Output
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.
Sample Input
42124
Sample Output
2(In the foregoing example piggy banks 1 and 4 have to be smashed.)
// Problem#: 1350// Submission#: 3304925// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University// Problem#: 1350// Submission#: 3304217// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <iostream>#include <vector>#include <algorithm>#include <stdio.h>#include <math.h>#include <string.h>#include <string>using namespace std;const int MAX_N = 1000005;int bank[MAX_N];int vis[MAX_N];int N;int main() { std::cout.sync_with_stdio(false); while (1) { cin >> N; if (cin.eof()) break; for (int i = 1; i <= N; i++) vis[i] = 0; for (int key = 1; key <= N; key++) { cin >> bank[key]; } int ans = 0; int color = 1; for (int i = 1; i <= N; i++) { if (vis[i]) continue; color++; int now = i; while (!vis[now]) { vis[now] = color; now = bank[now]; if (vis[now] == color) ans++; } } cout << ans << endl; } return 0;}
1350. Piggy banks
Constraints
Time Limit: 3 secs, Memory Limit: 32 MB
Description
Byteazar the Dragon has N 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.
Task
Write 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.
Input
注意:输入包含多个测试数据。
The first line of the standard input contains a single integer N (1 <= 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 1 to N. Next, there are N lines: the i+1st line contains a single integer - the number of the piggy bank in which the ith key has been placed.
Output
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.
Sample Input
42124
Sample Output
2(In the foregoing example piggy banks 1 and 4 have to be smashed.)
- Sicily 1350. Piggy banks
- sicily 1350. Piggy banks
- 1350. Piggy banks
- soj 1350. Piggy banks
- SOJ 1350. Piggy banks
- Sicily 1350 Piggy banks(搜索|求图中环的个数)
- POI2005:ska Piggy banks
- BZOJ1529ska Piggy banks
- BZOJ1529/POI2005 Piggy banks
- [POI2005]SKA-Piggy Banks
- bzoj1529【poi005】ska Piggy banks
- 【POI2005】【BZOJ1529】ska Piggy banks
- bzoj1529: [POI2005]ska Piggy banks
- bzoj1529: [POI2005]ska Piggy banks
- 1529: [POI2005]ska Piggy banks
- 【bzoj1529】 [POI2005]ska Piggy banks
- 1529: [POI2005]ska Piggy banks
- bzoj 1529: [POI2005]ska Piggy banks
- 走在一条怎样的道路上
- view.getX和view.getTranslationX的区别
- 数据库相对路径写法,及使用
- ftp 创建目录
- 自定义View之顶部导航栏
- Sicily 1350. Piggy banks
- JDK+MyEclipse+Tomcat的配置
- 打开CHM后无法连接到网页
- iOS开发中的图层-CALayer-
- java基础——图解集合
- java 堆栈的区别
- Java SE 第十一讲(面向对象之封装) 续二
- 这段时间装SDK装到吐血
- HDU 1024 — Max Sum Plus Plus