Language: C++

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Beg097D
{
    class Program
    {
        public static int[] pars;
        static void Main(string[] args)
        {
            var read = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
            var N = read[0];
            var M = read[1];

            var ps = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();

            var xs = new int[M];
            var ys = new int[M];

            for (int i = 0; i < M; i++)
            {
                read = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
                xs[i] = read[0];
                ys[i] = read[1];

            }

            pars = new int[N];
            for (int i = 0; i < N; i++) pars[i] = i;

            for(int i=0; i<M;i++)
            {
                if(pars[xs[i] - 1] != pars[ys[i] - 1])
                {
                    unit(xs[i] - 1, ys[i] - 1);
                }
            }
            int ans = 0;
            for(int i=0;i<N;i++)
            {
                if (root(ps[i] - 1) == root(i)) ans++;
            }

            Console.WriteLine(ans);
        }

        public static int root(int x)
        {
            return pars[x] == x ? x : root(pars[x]);
        }
        public static void unit(int x, int y)
        {
            var rx = root(x);
            var ry = root(y);
            if(rx != ry)
            {
                pars[ry] = rx;
            }
        }
    }
}


Compiler output:
a.cc:1:7: error: expected nested-name-specifier before 'System'
    1 | using System;
      |       ^~~~~~
a.cc:2:7: error: expected nested-name-specifier before 'System'
    2 | using System.Collections.Generic;
      |       ^~~~~~
a.cc:3:7: error: expected nested-name-specifier before 'System'
    3 | using System.Linq;
      |       ^~~~~~
a.cc:4:7: error: expected nested-name-specifier before 'System'
    4 | using System.Text;
      |       ^~~~~~
a.cc:5:7: error: expected nested-name-specifier before 'System'
    5 | using System.Threading.Tasks;
      |       ^~~~~~
a.cc:11:15: error: expected ':' before 'static'
   11 |         public static int[] pars;
      |               ^~~~~~~
      |               :
a.cc:11:26: error: expected unqualified-id before '[' token
   11 |         public static int[] pars;
      |                          ^
a.cc:12:26: error: 'string' has not been declared
   12 |         static void Main(string[] args)
      |                          ^~~~~~
a.cc:12:35: error: expected ',' or '...' before 'args'
   12 |         static void Main(string[] args)
      |                                   ^~~~
a.cc:50:15: error: expected ':' before 'static'
   50 |         public static int root(int x)
      |               ^~~~~~~
      |               :
a.cc:54:15: error: expected ':' before 'static'
   54 |         public static void unit(int x, int y)
      |               ^~~~~~~
      |               :
a.cc:63:6: error: expected ';' after class definition
   63 |     }
      |      ^
      |      ;
a.cc: In static member function 'static void Beg097D::Program::Main(int*)':
a.cc:14:13: error: 'var' was not declared in this scope
   14 |             var read = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
      |             ^~~
a.cc:15:17: error: expected ';' before 'N'
   15 |             var N = read[0];
      |                 ^
a.cc:16:17: error: expected ';' before 'M'
   16 |             var M = read[1];
      |                 ^
a.cc:18:17: error: expected ';' before 'ps'
   18 |             var ps = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
      |                 ^~
a.cc:20:17: error: expected ';' before 'xs'
   20 |             var xs = new int[M];
      |                 ^~
a.cc:21:17: error: expected ';' before 'ys'
   21 |             var ys = new int[M];
      |                 ^~
a.cc:23:33: error: 'M' was not declared in this scope
   23 |             for (int i = 0; i < M; i++)
      |                                 ^
a.cc:25:17: error: 'read' was not declared in this scope
   25 |                 read = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
      |                 ^~~~
a.cc:25:24: error: 'Console' was not declared in this scope
   25 |                 read = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
      |                        ^~~~~~~
a.cc:25:61: error: 'x' was not declared in this scope
   25 |                 read = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
      |                                                             ^
a.cc:25:64: error: expected primary-expression before '>' token
   25 |                 read = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
      |                                                                ^
a.cc:25:66: error: expected primary-expression before 'int'
   25 |                 read = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
      |                                                                  ^~~
a.cc:26:17: error: 'xs' was not declared in this scope
   26 |                 xs[i] = read[0];
      |                 ^~
a.cc:27:17: error: 'ys' was not declared in this scope
   27 |                 ys[i] = read[1];
      |                 ^~
a.cc:31:13: error: 'pars' was not declared in this scope
   31 |             pars = new int[N];
      |             ^~~~
a.cc:31:28: error: 'N' was not declared in this scope
   31 |             pars = new int[N];
      |                            ^
a.cc:34:28: error: 'M' was not declared in this scope
   34 |             for(int i=0; i<M;i++)
      |                            ^
a.cc:36:25: error: 'xs' was not declared in this scope
   36 |                 if(pars[xs[i] - 1] != pars[ys[i] - 1])
      |                         ^~
a.cc:36:44: error: 'ys' was not declared in this scope
   36 |                 if(pars[xs[i] - 1] != pars[ys[i] - 1])
      |                                            ^~
a.cc:44:26: error: 'ps' was not declared in this scope
   44 |                 if (root(ps[i] - 1) == root(i)) ans++;
      |                          ^~
a.cc:47:13: error: 'Console' was not declared in this scope
   47 |             Console.WriteLine(ans);
      |             ^~~~~~~
a.cc: In static member function 'static int Beg097D::Program::root(int)':
a.cc:52:20: error: 'pars' was not declared in this scope
   52 |             return pars[x] == x ? x : root(pars[x]);
      |                    ^~~~
a.cc: In static member function 'static void Beg097D::Program::unit(int, int)':
a.cc:56:13: error: 'var' was not declared in this scope
   56 |             var rx = root(x);
      |             ^~~
a.cc:57:17: error: expected ';' before 'ry'
   57 |             var ry = root(y);
      |                 ^~
a.cc:58:16: error: 'rx' was not declared in this scope; did you mean 'x'?
   58 |             if(rx != ry)
      |                ^~
      |                x
a.cc:58:22: error: 'ry' was not declared in this scope; did you mean 'y'?
   58 |             if(rx != ry)
      |                      ^~
      |                      y
a.cc:60:17: error: 'pars' was not declared in this scope
   60 |                 pars[ry] = rx;
      |                 ^~~~