結果

問題 No.1207 グラフX
ユーザー firiexpfiriexp
提出日時 2020-11-29 23:46:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 205 ms / 2,000 ms
コード長 3,582 bytes
コンパイル時間 1,422 ms
コンパイル使用メモリ 113,072 KB
実行使用メモリ 24,468 KB
最終ジャッジ日時 2023-10-11 02:40:56
合計ジャッジ時間 15,236 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 154 ms
17,368 KB
testcase_01 AC 159 ms
17,284 KB
testcase_02 AC 152 ms
17,376 KB
testcase_03 AC 158 ms
17,244 KB
testcase_04 AC 158 ms
17,348 KB
testcase_05 AC 197 ms
24,444 KB
testcase_06 AC 205 ms
24,332 KB
testcase_07 AC 200 ms
24,400 KB
testcase_08 AC 133 ms
11,396 KB
testcase_09 AC 138 ms
13,804 KB
testcase_10 AC 196 ms
20,552 KB
testcase_11 AC 202 ms
24,468 KB
testcase_12 AC 125 ms
12,232 KB
testcase_13 AC 67 ms
5,308 KB
testcase_14 AC 153 ms
16,444 KB
testcase_15 AC 139 ms
13,396 KB
testcase_16 AC 67 ms
5,972 KB
testcase_17 AC 108 ms
10,628 KB
testcase_18 AC 83 ms
11,148 KB
testcase_19 AC 100 ms
7,764 KB
testcase_20 AC 157 ms
16,932 KB
testcase_21 AC 11 ms
4,348 KB
testcase_22 AC 108 ms
10,836 KB
testcase_23 AC 119 ms
12,228 KB
testcase_24 AC 77 ms
10,292 KB
testcase_25 AC 154 ms
16,552 KB
testcase_26 AC 122 ms
13,000 KB
testcase_27 AC 151 ms
15,132 KB
testcase_28 AC 142 ms
13,728 KB
testcase_29 AC 139 ms
15,068 KB
testcase_30 AC 71 ms
7,740 KB
testcase_31 AC 56 ms
4,428 KB
testcase_32 AC 60 ms
8,004 KB
testcase_33 AC 64 ms
7,992 KB
testcase_34 AC 133 ms
13,276 KB
testcase_35 AC 12 ms
4,348 KB
testcase_36 AC 133 ms
13,784 KB
testcase_37 AC 116 ms
11,388 KB
testcase_38 AC 30 ms
5,352 KB
testcase_39 AC 67 ms
8,836 KB
testcase_40 AC 42 ms
4,352 KB
testcase_41 AC 98 ms
8,168 KB
testcase_42 AC 2 ms
4,352 KB
testcase_43 AC 2 ms
4,352 KB
testcase_44 AC 1 ms
4,348 KB
testcase_45 AC 149 ms
17,188 KB
testcase_46 AC 149 ms
17,156 KB
testcase_47 AC 148 ms
17,156 KB
testcase_48 AC 147 ms
17,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0