[10451] 순열 사이클

[10451] 순열 사이클

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

#include < stdio.h > #include < string .h > int n, a[ 1001 ]; bool visit[ 1001 ]; void f( int cur) { visit[cur] = true ; int i; int next = a[cur]; if (visit[next] = = false ) { f(next); } return ; } int main( void ) { int i,t; scanf ( "%d" , & t); while (t - - ) { scanf ( "%d" , & n); memset(visit, false , sizeof (visit)); for (i = 1 ; i < = n; i + + ) scanf ( "%d" , & a[i]); int cnt = 0 ; for (i = 1 ; i < = n; i + + ) { if (visit[i] = = false ) { f(i); cnt + + ; } } printf ( "%d

" , cnt); } return 0 ; } Colored by Color Scripter

from http://dizlet.tistory.com/20 by ccl(A) rewrite - 2021-08-30 18:26:08