結果

問題 No.2494 Sum within Components
ユーザー InTheBloomInTheBloom
提出日時 2023-10-06 22:00:55
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 302 ms / 2,000 ms
コード長 1,362 bytes
コンパイル時間 2,652 ms
コンパイル使用メモリ 162,516 KB
実行使用メモリ 26,244 KB
最終ジャッジ日時 2023-10-06 22:01:00
合計ジャッジ時間 5,162 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 21 ms
6,372 KB
testcase_10 AC 19 ms
5,340 KB
testcase_11 AC 7 ms
4,872 KB
testcase_12 AC 28 ms
7,000 KB
testcase_13 AC 16 ms
5,136 KB
testcase_14 AC 259 ms
23,132 KB
testcase_15 AC 258 ms
21,360 KB
testcase_16 AC 90 ms
18,652 KB
testcase_17 AC 91 ms
17,916 KB
testcase_18 AC 107 ms
15,668 KB
testcase_19 AC 302 ms
26,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

void main () {
    int N, M; readln.read(N, M);
    int[] A = readln.split.to!(int[]);
    int[][] graph = new int[][](N, 0);
    foreach (_; 0..M) {
        int U, V; readln.read(U, V);
        U--, V--;
        graph[U] ~= V;
        graph[V] ~= U;
    }

    solve(N, M, A, graph);
}

void solve (int N, int M, int[] A, int[][] graph) {
    // 連結成分を列挙していけばよいですね~
    const long MOD = 998244353;

    int[] visited = new int[](N);
    DList!int Q;
    long[] SumOfComponent;

    int idx = 0;
    visited[] = -1;

    foreach (i; 0..N) {
        if (visited[i] != -1) continue;
        visited[i] = idx;
        Q.insertBack(i);
        SumOfComponent ~= A[i];
        SumOfComponent[idx] %= MOD;

        while (!Q.empty) {
            auto head = Q.front; Q.removeFront;
            foreach (to; graph[head]) {
                if (visited[to] != -1) continue;
                visited[to] = idx;
                (SumOfComponent[idx] += A[to]) %= MOD;
                Q.insertBack(to);
            }
        }
        idx++;
    }

    long ans = 1;
    foreach (x; 0..N) {
        ans *= SumOfComponent[ visited[x] ];
        ans %= MOD;
    }

    writeln(ans);
}

void read(T...)(string S, ref T args) {
    auto buf = S.split;
    foreach (i, ref arg; args) {
        arg = buf[i].to!(typeof(arg));
    }
}
0