#include <iostream> #include <vector> using namespace std; class Cycle { private: vector<int> Relation; int Count = 0; public: Cycle(int n); void DFS(int target, int destination); int GetNumber(int index); void Print(); }; Cycle::Cycle(int n) { for (int i = 0; i < n; i++) { int Temp; cin >> Temp; this->Relation.push_back(Temp); } } void Cycle::DFS(int target, int destination) { if (target < 0) return; int t_temp = this->Relation[target] - 1; this->Relation[target] = -1; if (t_temp == destination) this->Count += 1; else DFS(t_temp, destination); } int Cycle::GetNumber(int index) { return this->Relation[index]; } void Cycle::Print() { cout << this->Count << endl; } int main() { int iTestCase; cin >> iTestCase; while (iTestCase--) { int N; cin >> N; Cycle C(N); for (int j = 0; j < N; j++) C.DFS(C.GetNumber(j) - 1, j); C.Print(); } }
# include < iostream > # include < vector > using namespace std ; int Num_of_Divisor ( int n ) { int Count = 0 ; for ( int i = 1 ; i < = n ; i + + ) if ( n % i = = 0 ) Count + + ; return Count ; } int main ( ) { int Testcase ; cin > > Testcase ; while ( Testcase - - ) { int Rooms ; cin > > Rooms ; vector < int > Prisons ; for ( int i = 0 ; i < Rooms ; i + + ) Prisons . push_back ( 0 ) ; for ( int i = 1 ; i < Prisons . size ( ) + 1 ; i + + ) { if ( ( Num_of_Divisor ( i ) % 2 ) = = 0 ) Prisons [ i - 1 ] = 0 ; else Prisons [ i - 1 ] = 1 ; } int Fleer = 0 ; for ( int i = 0 ; i < Prisons . size ( ) ; i + + ) if ( Prisons ...
댓글
댓글 쓰기