-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9466.cpp
More file actions
57 lines (48 loc) · 1.01 KB
/
9466.cpp
File metadata and controls
57 lines (48 loc) · 1.01 KB
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
#include<iostream>
#include<vector>
#include<cstring>
#define N 100005
using namespace std;
int n, map[N], check[N], inteam[N], visit[N], p, ans;
bool dfs(int index)
{
if(check[index]){
p=index;
return true;
}
if(visit[index]) return false;
visit[index]=1;
check[index]=1;
bool ret = dfs(map[index]);
check[index]=0;
if(!ret){
inteam[index]=0;
return false;
}
inteam[index]=1;
ans++;
if(index==p) return false;
return true;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int t, tc, i;
cin >> tc;
for(t=0; t<tc; t++){
cin >> n;
for(i=1; i<=n; i++) cin >> map[i];
memset(visit, 0, sizeof(visit));
for(i=1; i<=n; i++) inteam[i]=0;
ans=0;
for(i=1; i<=n; i++){
if(inteam[i] || visit[i]) continue;
dfs(i);
}
printf("%d\n", n-ans);
}
return 0;
}