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.)
0 0
原创粉丝点击