Sicily 1350. Piggy banks

本文描述了一位名叫Byteazar的龙拥有N个存钱罐,每个存钱罐可以用对应的钥匙打开或者砸碎。为了购买一辆汽车,Byteazar需要获取所有存钱罐中的金币,但他希望尽可能少地破坏存钱罐。文章提出了一种算法,用于确定必须砸碎多少存钱罐才能获得所有的金币。

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

4
2
1
2
4

Sample Output

2

(In the foregoing example piggy banks 1 and 4 have to be smashed.)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值