結果
| 問題 |
No.2494 Sum within Components
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-10-06 22:00:55 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 259 ms / 2,000 ms |
| コード長 | 1,362 bytes |
| コンパイル時間 | 4,457 ms |
| コンパイル使用メモリ | 180,584 KB |
| 実行使用メモリ | 25,320 KB |
| 最終ジャッジ日時 | 2024-07-26 16:06:47 |
| 合計ジャッジ時間 | 5,895 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 17 |
ソースコード
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));
}
}