-
Notifications
You must be signed in to change notification settings - Fork 8
/
Task N.cpp
49 lines (39 loc) · 951 Bytes
/
Task N.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void dfs (int i, int i_cur, vector<vector<int>> &piggy_bank, vector<bool> &used, vector<bool> &broke){
if (used[i]){
return;
}
used[i] = true;
for (int u : piggy_bank[i]){
if (u != i_cur){
dfs(u, i_cur, piggy_bank, used, broke);
broke[i] = false;
}
}
}
void solve(){
int n, key, i = 0;
cin >> n;
vector<vector<int>> piggy_bank(n);
vector<bool> used(n, false);
vector<bool> broke(n, true);
while (cin >> key){
piggy_bank[--key].push_back(i);
i++;
}
for (int i = 0; i < n; i++){
if (!used[i]){
dfs(i, i, piggy_bank, used, broke);
}
}
cout << count(broke.begin(), broke.end(), true);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
solve();
return 0;
}