結果

問題 No.2494 Sum within Components
ユーザー 👑 kakel-sankakel-san
提出日時 2023-10-06 21:55:58
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 332 ms / 2,000 ms
コード長 1,933 bytes
コンパイル時間 966 ms
コンパイル使用メモリ 66,260 KB
実行使用メモリ 46,524 KB
最終ジャッジ日時 2023-10-06 21:56:03
合計ジャッジ時間 4,840 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
19,904 KB
testcase_01 AC 64 ms
21,840 KB
testcase_02 AC 64 ms
19,940 KB
testcase_03 AC 64 ms
21,968 KB
testcase_04 AC 63 ms
23,924 KB
testcase_05 AC 63 ms
21,980 KB
testcase_06 AC 63 ms
21,776 KB
testcase_07 AC 63 ms
23,880 KB
testcase_08 AC 64 ms
23,896 KB
testcase_09 AC 91 ms
26,872 KB
testcase_10 AC 89 ms
27,036 KB
testcase_11 AC 77 ms
22,584 KB
testcase_12 AC 102 ms
30,424 KB
testcase_13 AC 86 ms
24,968 KB
testcase_14 AC 302 ms
42,228 KB
testcase_15 AC 327 ms
42,664 KB
testcase_16 AC 169 ms
41,380 KB
testcase_17 AC 170 ms
46,352 KB
testcase_18 AC 182 ms
46,220 KB
testcase_19 AC 332 ms
46,524 KB
権限があれば一括ダウンロードができます

ソースコード

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[] 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 a = NList;
        var map = NMap(m);
        var uf = new UnionFindTree(n);
        foreach (var edge in map)
        {
            uf.Unite(edge[0], edge[1]);
        }
        var sum = new long[n];
        for (var i = 0; i < n; ++i) sum[uf.GetRoot(i)] += a[i];
        var ans = 1L;
        var mod = 998_244_353;
        for (var i = 0; i < n; ++i) ans = ans * (sum[uf.GetRoot(i)] % mod) % mod;
        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