Language: C++

import java.util.*;
import java.util.stream.*;
import java.io.*;

class UnionFind {
  private int[] parent;
  private int[] rank;

  public int find(int i){
    int p = parent[i];
    if(i == p) return i;

    return parent[i] = find(p);
  }

  public void union(int i, int j){
    int root1 = find(i);
    int root2 = find(j);
    if(root1 == root2) return;

    if(rank[root1] > rank[root2]) parent[root2] = root1;
    else if(rank[root1] < rank[root2]) parent[root1] = root2;
    else parent[root2] = root1;
    rank[root1]++;
  }

  public UnionFind(int n){
    parent = new int[n];
    rank = new int[n];

    for(int i = 0; i < n; i++) parent[i] = i;
  }
}

public class Main {

  public static void main(String[] args) throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    String[] in1 = br.readLine().split(" ");
    int N = Integer.parseInt(in1[0]);
    int M = Integer.parseInt(in1[1]);
    
    in1 = br.readLine().split(" ");
    int[] p = Stream.of(in1).mapToInt(Integer::parseInt).toArray();
    
    for(int i = 0; i < N; i++){
      p[i]--;
    }

    int[][] xy = new int[M][2];

    for(int i = 0; i < M; i++){
      in1 = br.readLine().split(" ");
      xy[i] = Stream.of(in1).mapToInt(Integer::parseInt).toArray();
      xy[i][0]--; xy[i][1]--;
    }

    br.close();

    UnionFind uf = new UnionFind(N);

    for(int i = 0; i < M; i++){
      uf.union(p[xy[i][0]], p[xy[i][1]]);
    }

    List<List<Integer>> childList = new ArrayList<>();
    for(int i = 0; i < N; i++){
      childList.add(new ArrayList<Integer>());
    }

    for(int i = 0; i < N; i++){
      childList.get(uf.find(i)).add(i);
    }

    int[] idxArray = new int[N];
    for(int i = 0; i < N; i++){
      idxArray[p[i]] = i;
    }

    Set<Integer> idxSet = new HashSet<>();
    int ans = 0;

    for(int i = 0; i < N; i++){

      if(childList.get(i).isEmpty()) continue;
      
      idxSet.clear();
      for(int child : childList.get(i)){
        idxSet.add(idxArray[child]);
      }

      for(int child : childList.get(i)){
        if(idxSet.contains(child)){
          ans++;
        }
      }
    }

    System.out.println(ans);
  }
}

Compiler output:
a.cc:1:1: error: 'import' does not name a type
    1 | import java.util.*;
      | ^~~~~~
a.cc:1:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:2:1: error: 'import' does not name a type
    2 | import java.util.stream.*;
      | ^~~~~~
a.cc:2:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:3:1: error: 'import' does not name a type
    3 | import java.io.*;
      | ^~~~~~
a.cc:3:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:6:10: error: expected ':' before 'int'
    6 |   private int[] parent;
      |          ^~~~
      |          :
a.cc:6:14: error: expected unqualified-id before '[' token
    6 |   private int[] parent;
      |              ^
a.cc:7:10: error: expected ':' before 'int'
    7 |   private int[] rank;
      |          ^~~~
      |          :
a.cc:7:14: error: expected unqualified-id before '[' token
    7 |   private int[] rank;
      |              ^
a.cc:9:9: error: expected ':' before 'int'
    9 |   public int find(int i){
      |         ^~~~
      |         :
a.cc:16:9: error: expected ':' before 'void'
   16 |   public void union(int i, int j){
      |         ^~~~~
      |         :
a.cc:16:20: error: expected identifier before '(' token
   16 |   public void union(int i, int j){
      |                    ^
a.cc:16:21: error: expected unqualified-id before 'int'
   16 |   public void union(int i, int j){
      |                     ^~~
a.cc:16:21: error: expected ')' before 'int'
   16 |   public void union(int i, int j){
      |                    ~^~~
      |                     )
a.cc:27:9: error: expected ':' before 'UnionFind'
   27 |   public UnionFind(int n){
      |         ^~~~~~~~~~
      |         :
a.cc:33:2: error: expected ';' after class definition
   33 | }
      |  ^
      |  ;
a.cc: In member function 'int UnionFind::find(int)':
a.cc:10:13: error: 'parent' was not declared in this scope
   10 |     int p = parent[i];
      |             ^~~~~~
a.cc: In constructor 'UnionFind::UnionFind(int)':
a.cc:28:5: error: 'parent' was not declared in this scope
   28 |     parent = new int[n];
      |     ^~~~~~
a.cc:29:5: error: 'rank' was not declared in this scope
   29 |     rank = new int[n];
      |     ^~~~
a.cc: At global scope:
a.cc:35:1: error: expected unqualified-id before 'public'
   35 | public class Main {
      | ^~~~~~