結果
問題 | No.1207 グラフX |
ユーザー | srjywrdnprkt |
提出日時 | 2023-01-23 01:32:13 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 417 ms / 2,000 ms |
コード長 | 2,558 bytes |
コンパイル時間 | 1,746 ms |
コンパイル使用メモリ | 124,192 KB |
実行使用メモリ | 40,284 KB |
最終ジャッジ日時 | 2024-06-25 02:32:19 |
合計ジャッジ時間 | 19,677 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 336 ms
26,716 KB |
testcase_01 | AC | 328 ms
26,708 KB |
testcase_02 | AC | 333 ms
26,712 KB |
testcase_03 | AC | 333 ms
26,716 KB |
testcase_04 | AC | 334 ms
26,708 KB |
testcase_05 | AC | 417 ms
40,152 KB |
testcase_06 | AC | 411 ms
40,152 KB |
testcase_07 | AC | 400 ms
40,148 KB |
testcase_08 | AC | 282 ms
19,496 KB |
testcase_09 | AC | 289 ms
22,712 KB |
testcase_10 | AC | 399 ms
34,392 KB |
testcase_11 | AC | 417 ms
40,284 KB |
testcase_12 | AC | 271 ms
19,944 KB |
testcase_13 | AC | 195 ms
10,828 KB |
testcase_14 | AC | 325 ms
26,096 KB |
testcase_15 | AC | 293 ms
21,472 KB |
testcase_16 | AC | 180 ms
11,240 KB |
testcase_17 | AC | 229 ms
17,304 KB |
testcase_18 | AC | 177 ms
16,528 KB |
testcase_19 | AC | 257 ms
14,648 KB |
testcase_20 | AC | 322 ms
26,292 KB |
testcase_21 | AC | 36 ms
5,376 KB |
testcase_22 | AC | 231 ms
17,420 KB |
testcase_23 | AC | 248 ms
19,168 KB |
testcase_24 | AC | 159 ms
15,132 KB |
testcase_25 | AC | 335 ms
26,088 KB |
testcase_26 | AC | 251 ms
20,508 KB |
testcase_27 | AC | 324 ms
24,828 KB |
testcase_28 | AC | 293 ms
22,892 KB |
testcase_29 | AC | 286 ms
24,260 KB |
testcase_30 | AC | 161 ms
12,312 KB |
testcase_31 | AC | 170 ms
10,388 KB |
testcase_32 | AC | 127 ms
12,000 KB |
testcase_33 | AC | 141 ms
12,012 KB |
testcase_34 | AC | 297 ms
21,120 KB |
testcase_35 | AC | 36 ms
5,376 KB |
testcase_36 | AC | 287 ms
22,140 KB |
testcase_37 | AC | 250 ms
18,300 KB |
testcase_38 | AC | 70 ms
7,248 KB |
testcase_39 | AC | 141 ms
13,732 KB |
testcase_40 | AC | 146 ms
9,748 KB |
testcase_41 | AC | 231 ms
14,728 KB |
testcase_42 | AC | 2 ms
5,376 KB |
testcase_43 | AC | 2 ms
5,376 KB |
testcase_44 | AC | 2 ms
5,376 KB |
testcase_45 | AC | 322 ms
26,792 KB |
testcase_46 | AC | 308 ms
26,712 KB |
testcase_47 | AC | 319 ms
26,664 KB |
testcase_48 | AC | 307 ms
26,580 KB |
ソースコード
#include <iostream> #include <vector> #include <cmath> #include <map> #include <set> #include <iomanip> #include <queue> #include <algorithm> #include <numeric> #include <deque> using namespace std; struct UnionFind { vector<long long> par; vector<long long> siz; UnionFind(long long N) : par(N), siz(N) { for(long long i = 0; i < N; i++){ par[i] = i; siz[i] = 1; } } long long root(long long x) { if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(long long x, long long y) { long long rx = root(x); long long ry = root(y); if (rx == ry) return; par[rx] = ry; siz[ry] += siz[rx]; } bool same(long long x, long long y) { long long rx = root(x); long long ry = root(y); return rx == ry; } long long size(long long x){ return siz[root(x)]; } }; vector<long long> subtree; long long subtree_size(long long from, long long p, vector<vector<pair<long long, long long>>> &E){ long long cnt = 1; for (auto [to, C] : E[from]){ if (to == p) continue; cnt += subtree_size(to, from, E); } return subtree[from] = cnt; } const long long m = 1e9+7; long long mod_exp(long long b, long long e){ if (e > 0 && b == 0) return 0; long long ans = 1; b %= m; while (e > 0){ if ((e & 1LL)) ans = (ans * b) % m; e = e >> 1LL; b = (b*b) % m; } return ans; } long long ans=0, N, X; void solve(long long from, long long p, vector<vector<pair<long long, long long>>> &E){ for (auto [to, C] : E[from]){ if (to == p) continue; ans += ((subtree[to]*(N-subtree[to]))%m * mod_exp(X, C)) % m; ans %= m; solve(to, from, E); } } template<typename T> using pq = priority_queue<T, vector<T>, greater<T>>; int main(){ long long M, x, y, c; cin >> N >> M >> X; pq<tuple<long long, long long, long long>> que; UnionFind tree(N); vector<vector<pair<long long, long long>>> E(N); for (int i=0; i<M; i++){ cin >> x >> y >> c; x--; y--; que.push({c, x, y}); } while(!que.empty()){ tie(c, x, y) = que.top(); que.pop(); if (!tree.same(x, y)){ tree.unite(x, y); E[x].push_back({y, c}); E[y].push_back({x, c}); } } subtree.resize(N); subtree_size(0, -1, E); solve(0, -1, E); cout << ans << endl; return 0; }