0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-08 02:45:26 +00:00

P3420 [POI2005] SKA-Piggy Banks

https://www.luogu.com.cn/record/168548666
This commit is contained in:
Baoshuo Ren 2024-07-25 20:20:59 +08:00
parent e5bb7ab971
commit 25c1839311
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

47
Luogu/P3420/P3420.cpp Normal file
View File

@ -0,0 +1,47 @@
#include <iostream>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1e6 + 5;
int n, fa[N], cnt;
int find(int x) {
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void merge(int x, int y) {
x = find(x);
y = find(y);
if (x != y) {
fa[x] = y;
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++) {
fa[i] = i;
}
for (int i = 1, x; i <= n; i++) {
cin >> x;
merge(i, x);
}
for (int i = 1; i <= n; i++) {
if (fa[i] == i) cnt++;
}
cout << cnt << endl;
return 0;
}