結果
問題 | No.1502 Many Simple Additions |
ユーザー |
|
提出日時 | 2024-06-21 23:27:51 |
言語 | C# (.NET 8.0.404) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,212 bytes |
コンパイル時間 | 7,923 ms |
コンパイル使用メモリ | 166,288 KB |
実行使用メモリ | 185,984 KB |
最終ジャッジ日時 | 2024-06-21 23:28:08 |
合計ジャッジ時間 | 15,399 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 14 WA * 25 |
コンパイルメッセージ
復元対象のプロジェクトを決定しています... /home/judge/data/code/main.csproj を復元しました (98 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();public static void Main(){Solve();}static void Solve(){var c = NList;var (n, m, k) = (c[0], c[1], c[2]);var map = NArr(m);var tree = new List<(int to, long z)>[n];for (var i = 0; i < tree.Length; ++i) tree[i] = new List<(int to, long z)>();var uf = new UnionFindTree(n);foreach (var edge in map){uf.Unite(edge[0] - 1, edge[1] - 1);tree[edge[0] - 1].Add((edge[1] - 1, edge[2]));tree[edge[1] - 1].Add((edge[0] - 1, edge[2]));}var mod = 1_000_000_007;var ans1 = 1L;var ans2 = 1L;var visited1 = new bool[n];var visited2 = new bool[n];for (var i = 0; i < n; ++i) if (uf.GetRoot(i) == i){var min = 1L;var max = (long)k;DFS(i, k, 0, true, tree, visited1, ref min, ref max);ans1 = ans1 * (Math.Max(0, max - min + 1) % mod) % mod;min = 1L;max = k - 1;DFS(i, k - 1, 0, true, tree, visited2, ref min, ref max);ans2 = ans2 * (Math.Max(0, max - min + 1) % mod) % mod;}WriteLine((ans1 - ans2 + mod) % mod);}static void DFS(int cur, int k, long sum, bool minus, List<(int to, long z)>[] tree, bool[] visited, ref long min, ref long max){visited[cur] = true;foreach (var next in tree[cur]){if (visited[next.to]) continue;if (minus){max = Math.Min(max, next.z - sum - 1);min = Math.Max(min, next.z - sum - k);}else{min = Math.Max(min, 1 - next.z + sum);max = Math.Min(max, k - next.z + sum);}DFS(next.to, k, next.z - sum, !minus, tree, visited, ref min, ref max);}}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)];}}}