Exemple #1
0
        public void Solve()
        {
            UnionFind uf = new UnionFind((int) N);

            for (int i = 0; i < M; i++)
            {
                uf.Unite((int) X[i], (int) Y[i]);
            }

            long score = N;
            for (int i = 1; i <= N; i++)
            {
                if (uf.Root(i) != uf.Root((int) P[i - 1])) score--;
            }

            Console.WriteLine(score);
        }
Exemple #2
0
        public static void Main(string[] args)
        {
            int[] input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
            int   n     = input[0];
            int   m     = input[1];

            int[] p = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);

            int[] x = new int[m];
            int[] y = new int[m];
            for (int i = 0; i < m; i++)
            {
                int[] line = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
                x[i] = line[0];
                y[i] = line[1];
            }

            UnionFind union = new UnionFind(n);

            for (int i = 0; i < m; i++)
            {
                union.Unite(x[i], y[i]);
            }

            Point[] points = new Point[n + 1];
            for (int i = 0; i <= n; i++)
            {
                points[i] = new Point(i);
            }

            for (int i = 1; i <= n; i++)
            {
                points[union.Root(i)].Add(i);
            }

            int score = 0;

            foreach (Point point in points)
            {
                score += point.Score(p);
            }

            Console.WriteLine(score);
        }