-
Notifications
You must be signed in to change notification settings - Fork 1
/
10451.cpp
65 lines (53 loc) · 1.13 KB
/
10451.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cmath>
#include <cstring>
#include <bitset>
#define xx first
#define yy second
#define all(x) (x).begin(), (x).end()
using namespace std;
using i64 = long long int;
using ii = pair<int, int>;
using iis = pair<int, string>;
using ii64 = pair<i64, i64>;
using iii = tuple<int, int, int>;
void dfs(vector<int> &v, vector<bool> &visited, int start) {
if (visited[start])
return;
visited[start] = true;
dfs(v, visited, v[start]);
}
void solve() {
int n;
scanf("%d", &n);
vector<int> v(n + 1);
vector<bool> visited(n + 1);
for (int i = 1; i <= n; i++)
scanf("%d", &v[i]);
int ans = 0;
for (int i = 1; i <= n; i++) {
if (visited[i])
continue;
ans++;
dfs(v, visited, v[i]);
}
cout << ans << endl;
}
int main() {
int t;
scanf("%d", &t);
for (int i = 0; i < t; i++) {
solve();
}
return 0;
}