-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9466.cpp
More file actions
60 lines (60 loc) · 1.45 KB
/
Copy path9466.cpp
File metadata and controls
60 lines (60 loc) · 1.45 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
58
59
60
#include <iostream>
#include <vector>
#include <stack>
#include <string.h>
#define MAX 100001
using namespace std;
int student[MAX];
bool check[MAX]= {false,};
int dfs(int start){
stack<int> s;
check[start] = true;
s.push(start);
vector<int> v;
v.push_back(start);
while(!s.empty()){
int current_node = s.top();
s.pop();
int next_node = student[current_node];
if(!check[next_node]){
check[next_node] = true;
s.push(next_node);
v.push_back(next_node);
}else{
// 이 for문을 매번 비교했으니 시간초과 나는 수 밖에...
// else에다가 비교해서 시간초과를 면함. 히히
for(int i=0;i<v.size();i++){
if(v[i] == next_node){
return i;
}
}
}
}
return v.size();
}
int main(){
cin.tie(NULL);
cout.tie(NULL);
int t, n,ans;
cin >> t;
for(int i=0;i<t ;i++){
if(i != 0)
cout <<"\n";
cin >> n;
ans = 0;
for(int j =1;j<=n;j++){
cin >> student[j];
check[j] = false;
}
for(int j =1; j<=n;j++){
if(!check[j]){
int num =dfs(j);
// cout << "num: " << num << "\n";
// cout << j << "번째: " << num <<"\n";
ans += num;
}
}
cout << ans;
}
return 0;
}