結果
問題 |
No.2949 Product on Tree
|
ユーザー |
|
提出日時 | 2024-12-07 20:55:28 |
言語 | C# (.NET 8.0.404) |
結果 |
AC
|
実行時間 | 693 ms / 2,000 ms |
コード長 | 2,277 bytes |
コンパイル時間 | 19,693 ms |
コンパイル使用メモリ | 168,144 KB |
実行使用メモリ | 242,296 KB |
最終ジャッジ日時 | 2024-12-07 20:56:29 |
合計ジャッジ時間 | 41,569 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 46 |
コンパイルメッセージ
復元対象のプロジェクトを決定しています... /home/judge/data/code/main.csproj を復元しました (142 ms)。 MSBuild のバージョン 17.9.6+a4ecab324 (.NET) main -> /home/judge/data/code/bin/Release/net8.0/main.dll main -> /home/judge/data/code/bin/Release/net8.0/publish/
ソースコード
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 n = NN; var a = NList; var map = NMap(n - 1); var tree = new List<Pair>[n]; for (var i = 0; i < n; ++i) tree[i] = new List<Pair>(); foreach (var edge in map) { tree[edge[0]].Add(new Pair(edge[1])); tree[edge[1]].Add(new Pair(edge[0])); } DFS1(0, -1, a, tree); var ans = new long[n]; DFS2(0, -1, 0, a, tree, ans); var d = 0L; for (var i = 0; i < n; ++i) d = (d + ans[i] - a[i] + mod) % mod; WriteLine(d * 499_122_177 % mod); } class Pair { public int To; public long Num; public Pair(int to) { To = to; } } static int mod = 998_244_353; static long DFS1(int cur, int prev, int[] a, List<Pair>[] tree) { var sum = (long)a[cur]; foreach (var next in tree[cur]) { if (next.To == prev) continue; var sub = DFS1(next.To, cur, a, tree); next.Num = sub * a[cur] % mod; sum = (sum + next.Num) % mod; } return sum; } static void DFS2(int cur, int prev, long val, int[] a, List<Pair>[] tree, long[] ans) { var sum = (val * a[cur] + a[cur]) % mod; foreach (var next in tree[cur]) { if (next.To == prev) continue; sum = (sum + next.Num) % mod; } ans[cur] = sum; foreach (var next in tree[cur]) { if (next.To == prev) continue; DFS2(next.To, cur, (sum - next.Num + mod) % mod, a, tree, ans); } } }