Submission #3926161


Source Code Expand

using System;
using System.Collections.Generic;
using System.Linq;
using static Atcoder;
using System.Threading.Tasks;
using System.ComponentModel;

public class Hello
{
    private const int INF = 1000000007;
    public static void Main()
    {
        int[] NM = IntLine();
        int N = NM[0], M = NM[1];
        int[] ps = IntLine();
        UnionFind tree = new UnionFind(N);

        for (int i = 0; i < M; i++)
        {
            int[] box = IntLine().Select(v => v - 1).ToArray(); ;
            tree.Unite(box[0], box[1]);
        }

        int ans = 0;
        for (int i = 0; i < N; i++)
        {
            if (tree.Same(i, ps[i] - 1)) ans++;
        }
        Console.WriteLine(ans);
    }
}

public class UnionFind
{
    public List<int> parent = new List<int>();

    public UnionFind(int N)
    {
        for (int i = 0; i < N; i++) parent.Add(i);
    }

    private int Root(int a)
    {
        if (parent[a] == a) return a;
        else return parent[a] = Root(parent[a]);
    }

    public void Unite(int a, int b)
    {
        a = Root(a);
        b = Root(b);
        if (a == b) return;
        parent[a] = parent[b];
    }

    public bool Same(int a, int b) { return (Root(a) == Root(b)); }
}

public class Atcoder
{
    public static void Swap<T>(ref T a, ref T b)
    {
        var t = a;
        a = b;
        b = t;
    }
    public static int LCM(int a, int b)
    {
        int c = a * b;
        if (a < b) Swap(ref a, ref b);
        int d = a % b;
        while (d != 0)
        {
            a = b;
            b = d;
            d = a % b;
        }
        return c / b;
    }
    public static string StrReverce(string s)
    {
        return new string(s.Reverse().ToArray());
    }
    public static int IntRead()
    {
        return int.Parse(Console.ReadLine());
    }
    public static bool IsPrime(int n)
    {
        if (n < 2) return false;
        if (n == 2) return true;
        if (n % 2 == 0) return false;

        //double sqrtNum = Math.Sqrt(n);
        for (int i = 3; i < n; i += 2)
        {
            if (n % i == 0) return false;
        }
        return true;
    }
    public static string[] SpRead(char c)
    {
        return Console.ReadLine().Split(c);
    }
    public static long[] LSRead()
    {
        return Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
    }
    public static long Combination(int n, int r)
    {
        if (n == r) return 1;
        return
            Enumerable.Range((n - r + 1), r).ToList().Aggregate((now, next) => now * next) /
            Enumerable.Range(1, r).ToList().Aggregate((now, next) => now * next);
    }
    public static long Permutation(int n, int r)
    {
        if (n == r) return 1;
        return
            Enumerable.Range((n - r), r).ToList().Aggregate((now, next) => now * next);
    }
    public static long LongRead()
    {
        return long.Parse(Console.ReadLine());
    }
    public static long[] LongLine()
    {
        return Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
    }
    public static int[] IntLine()
    {
        return Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
    }
}

Submission Info

Submission Time
Task D - Equals
User chika_haru
Language C# (Mono 4.6.2.0)
Score 400
Code Size 3305 Byte
Status AC
Exec Time 212 ms
Memory 28476 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 26 ms 11348 KB
0_001.txt AC 26 ms 11348 KB
0_002.txt AC 26 ms 13396 KB
0_003.txt AC 25 ms 9300 KB
1_004.txt AC 151 ms 15392 KB
1_005.txt AC 198 ms 24384 KB
1_006.txt AC 201 ms 22332 KB
1_007.txt AC 26 ms 11348 KB
1_008.txt AC 25 ms 9300 KB
1_009.txt AC 25 ms 9300 KB
1_010.txt AC 26 ms 13396 KB
1_011.txt AC 25 ms 9300 KB
1_012.txt AC 25 ms 9300 KB
1_013.txt AC 27 ms 13396 KB
1_014.txt AC 32 ms 11348 KB
1_015.txt AC 26 ms 11316 KB
1_016.txt AC 27 ms 11316 KB
1_017.txt AC 28 ms 13364 KB
1_018.txt AC 134 ms 15492 KB
1_019.txt AC 63 ms 19168 KB
1_020.txt AC 64 ms 19168 KB
1_021.txt AC 64 ms 17120 KB
1_022.txt AC 212 ms 28476 KB