[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