Submission #2499280


Source Code Expand

Copy
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    int n, m;
    int[] ps, xs, ys;

    public static void main(String args[]) {
        new Main().run();
    }

    void run() {
        FastReader sc = new FastReader();
        n = sc.nextInt();
        m = sc.nextInt();
        ps = new int[n];
        xs = new int[m];
        ys = new int[m];
        for (int i = 0; i < n; i++) {
            ps[i] = sc.nextInt() - 1;
        }
        for (int i = 0; i < m; i++) {
            xs[i] = sc.nextInt() - 1;
            ys[i] = sc.nextInt() - 1;
        }
        solve();
    }

    void solve() {
        UnionFind uf = new UnionFind(n);
        for (int i = 0; i < m; i++) {
            uf.unite(xs[i], ys[i]);
        }
        int count = 0;
        for (int i = 0; i < n; i++) {
            if (!uf.differ(i, ps[i])) {
                count++;
            }
        }
        System.out.println(count);
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

    class UnionFind {
        int[] parents;
        // only roots have correct counts
        int[] counts;

        public UnionFind(int size) {
            parents = new int[size];
            counts = new int[size];
            for (int i = 0; i < size; i++) {
                parents[i] = i;
                counts[i] = 1;
            }
        }

        // return the root of the input
        // adopt path compression
        public int find(int a) {
            if (parents[a] == a) return a;
            parents[a] = find(parents[a]);
            return parents[a];
        }

        // adopt weighted union rule
        public void unite(int a, int b) {
            a = find(a);
            b = find(b);
            if (a == b) return;
            if (counts[a] < counts[b]) {
                parents[a] = b;
                counts[b] += counts[a];
            } else {
                parents[b] = a;
                counts[a] += counts[b];
            }
        }

        public boolean differ(int a, int b) {
            a = find(a);
            b = find(b);
            return a != b;
        }

        public int count(int a) {
            return counts[find(a)];
        }
    }

}

Submission Info

Submission Time
Task D - Equals
User ynish
Language Java8 (OpenJDK 1.8.0)
Score 400
Code Size 3521 Byte
Status AC
Exec Time 305 ms
Memory 46468 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 4
AC × 23
Set Name Test Cases
Sample 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt
All 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 1_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt
Case Name Status Exec Time Memory
0_000.txt AC 84 ms 19540 KB
0_001.txt AC 73 ms 20820 KB
0_002.txt AC 84 ms 21204 KB
0_003.txt AC 82 ms 17492 KB
1_004.txt AC 205 ms 41700 KB
1_005.txt AC 279 ms 46468 KB
1_006.txt AC 305 ms 42872 KB
1_007.txt AC 72 ms 18900 KB
1_008.txt AC 74 ms 21460 KB
1_009.txt AC 73 ms 18644 KB
1_010.txt AC 83 ms 18516 KB
1_011.txt AC 74 ms 18772 KB
1_012.txt AC 73 ms 21076 KB
1_013.txt AC 84 ms 19540 KB
1_014.txt AC 107 ms 21972 KB
1_015.txt AC 81 ms 21460 KB
1_016.txt AC 79 ms 19412 KB
1_017.txt AC 88 ms 21588 KB
1_018.txt AC 229 ms 39220 KB
1_019.txt AC 177 ms 32704 KB
1_020.txt AC 182 ms 35440 KB
1_021.txt AC 189 ms 34464 KB
1_022.txt AC 289 ms 42988 KB