結果
問題 | No.1207 グラフX |
ユーザー | firiexp |
提出日時 | 2020-11-29 23:46:21 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 203 ms / 2,000 ms |
コード長 | 3,582 bytes |
コンパイル時間 | 1,231 ms |
コンパイル使用メモリ | 114,820 KB |
実行使用メモリ | 24,696 KB |
最終ジャッジ日時 | 2024-09-13 02:12:23 |
合計ジャッジ時間 | 12,407 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 150 ms
17,460 KB |
testcase_01 | AC | 150 ms
17,468 KB |
testcase_02 | AC | 149 ms
17,488 KB |
testcase_03 | AC | 154 ms
17,472 KB |
testcase_04 | AC | 155 ms
17,468 KB |
testcase_05 | AC | 203 ms
24,576 KB |
testcase_06 | AC | 200 ms
24,688 KB |
testcase_07 | AC | 202 ms
24,680 KB |
testcase_08 | AC | 129 ms
11,412 KB |
testcase_09 | AC | 132 ms
13,988 KB |
testcase_10 | AC | 193 ms
20,772 KB |
testcase_11 | AC | 200 ms
24,696 KB |
testcase_12 | AC | 123 ms
12,472 KB |
testcase_13 | AC | 65 ms
6,944 KB |
testcase_14 | AC | 149 ms
16,716 KB |
testcase_15 | AC | 131 ms
13,484 KB |
testcase_16 | AC | 66 ms
6,944 KB |
testcase_17 | AC | 101 ms
11,124 KB |
testcase_18 | AC | 82 ms
11,284 KB |
testcase_19 | AC | 96 ms
8,192 KB |
testcase_20 | AC | 151 ms
17,260 KB |
testcase_21 | AC | 11 ms
6,940 KB |
testcase_22 | AC | 103 ms
11,240 KB |
testcase_23 | AC | 112 ms
12,384 KB |
testcase_24 | AC | 71 ms
10,416 KB |
testcase_25 | AC | 147 ms
16,960 KB |
testcase_26 | AC | 115 ms
13,444 KB |
testcase_27 | AC | 138 ms
15,260 KB |
testcase_28 | AC | 135 ms
13,912 KB |
testcase_29 | AC | 132 ms
15,368 KB |
testcase_30 | AC | 69 ms
7,936 KB |
testcase_31 | AC | 56 ms
6,944 KB |
testcase_32 | AC | 58 ms
8,320 KB |
testcase_33 | AC | 61 ms
8,320 KB |
testcase_34 | AC | 133 ms
13,352 KB |
testcase_35 | AC | 13 ms
6,944 KB |
testcase_36 | AC | 127 ms
13,964 KB |
testcase_37 | AC | 111 ms
11,688 KB |
testcase_38 | AC | 28 ms
6,940 KB |
testcase_39 | AC | 63 ms
8,840 KB |
testcase_40 | AC | 43 ms
6,940 KB |
testcase_41 | AC | 88 ms
8,448 KB |
testcase_42 | AC | 2 ms
6,940 KB |
testcase_43 | AC | 2 ms
6,940 KB |
testcase_44 | AC | 2 ms
6,944 KB |
testcase_45 | AC | 143 ms
17,448 KB |
testcase_46 | AC | 146 ms
17,460 KB |
testcase_47 | AC | 144 ms
17,452 KB |
testcase_48 | AC | 144 ms
17,448 KB |
ソースコード
#include <iostream> #include <algorithm> #include <map> #include <set> #include <queue> #include <stack> #include <numeric> #include <bitset> #include <cmath> static const int MOD = 1000000007; using ll = long long; using u32 = unsigned; using u64 = unsigned long long; using namespace std; template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208; template <u32 M> struct modint { u32 val; public: static modint raw(int v) { modint x; x.val = v; return x; } modint() : val(0) {} template <class T> modint(T v) { ll x = (ll)(v%(ll)(M)); if (x < 0) x += M; val = u32(x); } modint(bool v) { val = ((unsigned int)(v) % M); } modint& operator++() { val++; if (val == M) val = 0; return *this; } modint& operator--() { if (val == 0) val = M; val--; return *this; } modint operator++(int) { modint result = *this; ++*this; return result; } modint operator--(int) { modint result = *this; --*this; return result; } modint& operator+=(const modint& b) { val += b.val; if (val >= M) val -= M; return *this; } modint& operator-=(const modint& b) { val -= b.val; if (val >= M) val += M; return *this; } modint& operator*=(const modint& b) { u64 z = val; z *= b.val; val = (u32)(z % M); return *this; } modint& operator/=(const modint& b) { return *this = *this * b.inv(); } modint operator+() const { return *this; } modint operator-() const { return modint() - *this; } modint pow(long long n) const { modint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } modint inv() const { return pow(M-2); } friend modint operator+(const modint& a, const modint& b) { return modint(a) += b; } friend modint operator-(const modint& a, const modint& b) { return modint(a) -= b; } friend modint operator*(const modint& a, const modint& b) { return modint(a) *= b; } friend modint operator/(const modint& a, const modint& b) { return modint(a) /= b; } friend bool operator==(const modint& a, const modint& b) { return a.val == b.val; } friend bool operator!=(const modint& a, const modint& b) { return a.val != b.val; } }; using mint = modint<MOD>; class UnionFind { int n; vector<int> uni; int forest_size; public: explicit UnionFind(int n) : n(n), uni(static_cast<u32>(n), -1), forest_size(n) {}; int root(int a){ if (uni[a] < 0) return a; else return (uni[a] = root(uni[a])); } bool unite(int a, int b) { a = root(a); b = root(b); if(a == b) return false; if(uni[a] > uni[b]) swap(a, b); uni[a] += uni[b]; uni[b] = a; forest_size--; return true; } int size(){ return forest_size; } int size(int i){ return -uni[root(i)]; } bool same(int a, int b) { return root(a) == root(b); } }; int main() { int n, m, t; cin >> n >> m >> t; UnionFind uf(n); mint ans = 0; vector<vector<pair<int, int>>> G(n); for (int i = 0; i < m; ++i) { int x, y, z; scanf("%d %d %d", &x, &y, &z); x--; y--; if(uf.root(x) != uf.root(y)){ G[x].emplace_back(y, z); G[y].emplace_back(x, z); uf.unite(x, y); } } vector<mint> sub(n, 1); auto dfs = [&](int x, int par, auto &&dfs) -> void { for (auto [y, w] : G[x]) { if(y == par) continue; dfs(y, x, dfs); sub[x] += sub[y]; ans += sub[y]*(n-sub[y])*mint(t).pow(w); } }; dfs(0, -1, dfs); cout << ans.val << "\n"; return 0; }