結果
問題 | No.1207 グラフX |
ユーザー | nebukuro09 |
提出日時 | 2020-08-30 13:53:44 |
言語 | D (dmd 2.107.1) |
結果 |
AC
|
実行時間 | 809 ms / 2,000 ms |
コード長 | 2,881 bytes |
コンパイル時間 | 1,215 ms |
コンパイル使用メモリ | 125,264 KB |
実行使用メモリ | 57,264 KB |
最終ジャッジ日時 | 2023-09-04 09:38:31 |
合計ジャッジ時間 | 33,954 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge14 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 709 ms
28,584 KB |
testcase_01 | AC | 709 ms
26,068 KB |
testcase_02 | AC | 701 ms
33,400 KB |
testcase_03 | AC | 700 ms
27,988 KB |
testcase_04 | AC | 705 ms
28,628 KB |
testcase_05 | AC | 807 ms
55,504 KB |
testcase_06 | AC | 805 ms
56,284 KB |
testcase_07 | AC | 800 ms
55,484 KB |
testcase_08 | AC | 502 ms
22,112 KB |
testcase_09 | AC | 579 ms
25,724 KB |
testcase_10 | AC | 804 ms
43,384 KB |
testcase_11 | AC | 809 ms
57,264 KB |
testcase_12 | AC | 520 ms
23,688 KB |
testcase_13 | AC | 218 ms
9,080 KB |
testcase_14 | AC | 672 ms
26,332 KB |
testcase_15 | AC | 569 ms
24,264 KB |
testcase_16 | AC | 232 ms
11,124 KB |
testcase_17 | AC | 431 ms
18,800 KB |
testcase_18 | AC | 395 ms
17,556 KB |
testcase_19 | AC | 362 ms
14,528 KB |
testcase_20 | AC | 679 ms
26,576 KB |
testcase_21 | AC | 32 ms
5,932 KB |
testcase_22 | AC | 431 ms
18,380 KB |
testcase_23 | AC | 483 ms
21,112 KB |
testcase_24 | AC | 347 ms
17,020 KB |
testcase_25 | AC | 686 ms
31,212 KB |
testcase_26 | AC | 519 ms
23,684 KB |
testcase_27 | AC | 630 ms
28,348 KB |
testcase_28 | AC | 576 ms
23,172 KB |
testcase_29 | AC | 601 ms
25,216 KB |
testcase_30 | AC | 278 ms
14,740 KB |
testcase_31 | AC | 175 ms
7,804 KB |
testcase_32 | AC | 259 ms
14,192 KB |
testcase_33 | AC | 265 ms
13,460 KB |
testcase_34 | AC | 551 ms
25,460 KB |
testcase_35 | AC | 36 ms
5,936 KB |
testcase_36 | AC | 551 ms
22,960 KB |
testcase_37 | AC | 461 ms
21,172 KB |
testcase_38 | AC | 115 ms
7,208 KB |
testcase_39 | AC | 280 ms
14,388 KB |
testcase_40 | AC | 120 ms
6,268 KB |
testcase_41 | AC | 339 ms
17,500 KB |
testcase_42 | AC | 1 ms
4,376 KB |
testcase_43 | AC | 1 ms
4,380 KB |
testcase_44 | AC | 2 ms
4,380 KB |
testcase_45 | AC | 683 ms
26,260 KB |
testcase_46 | AC | 684 ms
26,780 KB |
testcase_47 | AC | 678 ms
24,008 KB |
testcase_48 | AC | 676 ms
26,040 KB |
ソースコード
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdlib, std.datetime; immutable long MOD = 10^^9 + 7; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto X = s[2].to!long; long score(long c) { return powmod(X, c, MOD); } Tuple!(int, int, long)[] E; foreach (_; 0..M) { s = readln.split.map!(to!int); auto a = s[0] - 1; auto b = s[1] - 1; auto c = s[2].to!long; E ~= tuple(a, b, c); } E.sort!"a[2] < b[2]"; auto uf = new UnionFind(N); auto G = new Tuple!(int, long)[][](N); foreach (e; E) { auto a = e[0]; auto b = e[1]; auto c = e[2]; if (uf.find(a) == uf.find(b)) continue; uf.unite(a, b); G[a] ~= tuple(b, c); G[b] ~= tuple(a, c); } auto sub = new int[](N); void dfs1(int n, int p) { sub[n] = 1; foreach (e; G[n]) { auto m = e[0]; if (m != p) { dfs1(m, n); sub[n] += sub[m]; } } } dfs1(0, -1); auto dp = new long[](N); void dfs2(int n, int p) { foreach (e; G[n]) { auto m = e[0]; auto c = e[1]; if (m != p) { dfs2(m, n); dp[n] += dp[m] + sub[m] * score(c) % MOD; dp[n] %= MOD; } } } dfs2(0, -1); auto ans = new long[](N); void dfs3(int n, int p, long cp) { if (p == -1) { ans[n] = dp[n]; } else { ans[n] = ans[p] - sub[n] * score(cp) % MOD + (N - sub[n]) * score(cp) % MOD; ans[n] = (ans[n] % MOD + MOD) % MOD; } foreach (e; G[n]) { auto m = e[0]; auto c = e[1]; if (m != p) dfs3(m, n, c); } } dfs3(0, -1, 0); long ansans = 0; foreach (a; ans) (ansans += a) %= MOD; ansans = ansans * powmod(2, MOD-2, MOD) % MOD; ansans.writeln; } class UnionFind { import std.algorithm : swap; int n; int[] table; this(int n) { this.n = n; table = new int[](n); table[] = -1; } int find(int x) { return table[x] < 0 ? x : (table[x] = find(table[x])); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (table[x] > table[y]) swap(x, y); table[x] += table[y]; table[y] = x; } bool same(int x, int y) { return find(x) == find(y); } } long powmod(long a, long x, long m) { long ret = 1; while (x) { if (x % 2) ret = ret * a % m; a = a * a % m; x /= 2; } return ret; }