結果

問題 No.2563 色ごとのグループ
ユーザー 👑 kakel-sankakel-san
提出日時 2023-12-02 14:59:42
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 315 ms / 2,000 ms
コード長 2,056 bytes
コンパイル時間 6,550 ms
コンパイル使用メモリ 157,764 KB
実行使用メモリ 229,072 KB
最終ジャッジ日時 2023-12-02 15:00:07
合計ジャッジ時間 13,713 ms
ジャッジサーバーID
(参考情報)
judge13 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
32,676 KB
testcase_01 AC 73 ms
32,676 KB
testcase_02 AC 73 ms
32,676 KB
testcase_03 AC 84 ms
32,676 KB
testcase_04 AC 74 ms
32,676 KB
testcase_05 AC 72 ms
32,932 KB
testcase_06 AC 73 ms
32,676 KB
testcase_07 AC 72 ms
32,932 KB
testcase_08 AC 73 ms
32,676 KB
testcase_09 AC 74 ms
32,932 KB
testcase_10 AC 72 ms
32,548 KB
testcase_11 AC 74 ms
32,932 KB
testcase_12 AC 73 ms
32,932 KB
testcase_13 AC 73 ms
32,676 KB
testcase_14 AC 75 ms
33,316 KB
testcase_15 AC 75 ms
33,444 KB
testcase_16 AC 86 ms
33,444 KB
testcase_17 AC 76 ms
33,444 KB
testcase_18 AC 74 ms
33,060 KB
testcase_19 AC 78 ms
34,724 KB
testcase_20 AC 86 ms
37,028 KB
testcase_21 AC 80 ms
35,492 KB
testcase_22 AC 79 ms
35,236 KB
testcase_23 AC 87 ms
37,796 KB
testcase_24 AC 181 ms
63,048 KB
testcase_25 AC 165 ms
62,988 KB
testcase_26 AC 217 ms
67,008 KB
testcase_27 AC 146 ms
61,588 KB
testcase_28 AC 204 ms
67,696 KB
testcase_29 AC 277 ms
82,024 KB
testcase_30 AC 277 ms
82,028 KB
testcase_31 AC 303 ms
82,024 KB
testcase_32 AC 275 ms
82,024 KB
testcase_33 AC 289 ms
82,088 KB
testcase_34 AC 289 ms
81,748 KB
testcase_35 AC 315 ms
81,748 KB
testcase_36 AC 310 ms
82,216 KB
testcase_37 AC 289 ms
229,072 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (91 ms)。
MSBuild のバージョン 17.7.3+8ec440e68 (.NET)
  main -> /home/judge/data/code/bin/Release/net7.0/main.dll
  main -> /home/judge/data/code/bin/Release/net7.0/publish/

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
    static int[] NMi => ReadLine().Split().Select(c => int.Parse(c) - 1).ToArray();
    static int[][] NMap(int n) => Enumerable.Repeat(0, n).Select(_ => NMi).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (n, m) = (c[0], c[1]);
        var col = NList;
        var map = NMap(m);
        var uf = new UnionFindTree(n);
        foreach (var edge in map)
        {
            if (col[edge[0]] == col[edge[1]]) uf.Unite(edge[0], edge[1]);
        }
        var exists = new bool[n];
        var ans = 0;
        for (var i = 0; i < n; ++i) if (uf.GetRoot(i) == i)
        {
            if (exists[col[i] - 1]) ++ans;
            else exists[col[i] - 1] = true;
        }
        WriteLine(ans);
    }
    class UnionFindTree
    {
        int[] roots;
        public UnionFindTree(int size)
        {
            roots = new int[size];
            for (var i = 0; i < size; ++i) roots[i] = -1;
        }
        public int GetRoot(int a)
        {
            if (roots[a] < 0) return a;
            return roots[a] = GetRoot(roots[a]);
        }
        public bool IsSameTree(int a, int b)
        {
            return GetRoot(a) == GetRoot(b);
        }
        public bool Unite(int a, int b)
        {
            var x = GetRoot(a);
            var y = GetRoot(b);
            if (x == y) return false;
            if (-roots[x] < -roots[y]) { var tmp = x; x = y; y = tmp; }
            roots[x] += roots[y];
            roots[y] = x;
            return true;
        }
        public int GetSize(int a)
        {
            return -roots[GetRoot(a)];
        }
    }
}
0