[BZOJ1529] [Poi2005]ska Piggy Banks

Source: Internet
Author: User

[BZOJ1529] [Poi2005]ska Piggy Banks

Question Description

Byteazar has N Piggy piggy banks. Each piggy bank can only be opened with a key or smashed open. Byteazar has put the keys to each piggy bank in some piggy banks. Byteazar now wants to buy a car so he has to take all the money out. He wanted to break the piggy bank and take out all the money and ask at least how many piggy banks to break.

Input

The first line an integer n (1 <= n <= 1.000.000) – Represents the total number of piggy banks. Next each line is an integer, and the integer of line i+1 represents the piggy bank number that the key of the deposit jar is placed on.

Output

An integer means the minimum number of piggy banks to break.

Input example

4 2 1 2 4

Output example

2

Data size and conventions

See " Input "

Exercises

Add edge to find the number of connected blocks.

#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <stack > #include <vector> #include <queue> #include <cstring> #include <string> #include <map > #include <set>using namespace std;const int buffersize = 1 << 16;char buffer[buffersize], *head, *TAIL;INL ine Char Getchar () {if (Head = = Tail) {int L = fread (buffer, 1, buffersize, stdin); Tail = (Head = buffer) + L;} return *head++;} int read () {int x = 0, F = 1, char c = Getchar (), while (!isdigit (c)) {if (c = = '-') f =-1; c = Getchar ();} while (IsDigit (c)) {x = x * + C-' 0 '; c = Getchar ();} return x * f;} #define MAXN 1000010int N, Fa[maxn], pos[maxn];int findset (int x) {return x = = Fa[x]? x:fa[x] = Findset (Fa[x]);} int main () {n = read (), for (int i = 1; I <= n; i++) Fa[i] = i;for (int i = 1; I <= n; i++) {int u = findset (i), V = fi Ndset (read ()); if (U = v) fa[v] = u;} for (int i = 1; I <= n; i++) pos[i] = Findset (i); sort (pos + 1, pos + n + 1); int cnt = 0;for (int i = 1; I <= n; i++) if (i = = 1 | | pos[i]! = pos[i-1]) cnt++;p rintf ("%d\n", CNT); return 0;} 

[BZOJ1529] [Poi2005]ska Piggy Banks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.