#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <map>
#include <vector>
using namespace std;
 
int T;
int N;
vector<int> graph[1010];
int visited[1010];
 
void DFS(int start)
{
    visited[start] = 1;
    
    for(int i = 0; i < graph[start].size(); i++)
    {
        if(visited[graph[start][i]] == 0)
        {
            DFS(graph[start][i]);
        }
    }    
}
 
int main(void)
{
//    freopen("B10451_input.txt", "r", stdin);
    
    cin >> T;
    
    for(int k = 1; k <= T; k++)
    {
        for(int i = 1; i <= 1000; i++)
        {
            graph[i].clear();
        }
        memset(visited, 0sizeof(visited));
        
        cin >> N;
        
        for(int a = 1; a <= N; a++)
        {
            int b;
            cin >> b;
            
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        
        int cnt = 0;
        for(int i = 1; i <= N; i++)
        {
            if(visited[i] == 0)
            {
                DFS(i);
                
                cnt++;
            }
        }
        
        cout << cnt << endl;
    }
    
    return 0;
}
cs

+ Recent posts