結果
問題 | No.1207 グラフX |
ユーザー |
|
提出日時 | 2025-04-27 20:23:17 |
言語 | C# (.NET 8.0.404) |
結果 |
AC
|
実行時間 | 670 ms / 2,000 ms |
コード長 | 2,906 bytes |
コンパイル時間 | 9,105 ms |
コンパイル使用メモリ | 169,964 KB |
実行使用メモリ | 228,792 KB |
最終ジャッジ日時 | 2025-04-27 20:23:54 |
合計ジャッジ時間 | 37,062 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 46 |
コンパイルメッセージ
復元対象のプロジェクトを決定しています... /home/judge/data/code/main.csproj を復元しました (115 ミリ秒)。 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, x) = (c[0], c[1], c[2]); var map = NArr(m); var tree = new List<(int to, int len)>[n]; for (var i = 0; i < n; ++i) tree[i] = new List<(int to, int len)>(); var uf = new UnionFindTree(n); for (var i = 0; i < m; ++i) { var u = map[i][0] - 1; var v = map[i][1] - 1; if (uf.Unite(u, v)) { tree[u].Add((v, map[i][2])); tree[v].Add((u, map[i][2])); } } var size = new int[n]; WriteLine(DFS(0, -1, n, x, tree, size)); } static int mod = 1_000_000_007; static long DFS(int cur, int prev, int n, int x, List<(int to, int len)>[] tree, int[] size) { var ans = 0L; var stmp = 1; foreach (var next in tree[cur]) { if (prev == next.to) continue; ans = (ans + DFS(next.to, cur, n, x, tree, size)) % mod; stmp += size[next.to]; ans = (ans + Exp(x, next.len, mod) * size[next.to] % mod * (n - size[next.to]) % mod) % mod; } size[cur] = stmp; return ans; } static long Exp(long n, long p, int mod) { long _n = n % mod; var _p = p; var result = 1L; if ((_p & 1) == 1) result *= _n; while (_p > 0) { _n = _n * _n % mod; _p >>= 1; if ((_p & 1) == 1) result = result * _n % mod; } return result; } 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)]; } } }