結果

問題 No.1207 グラフX
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-08-31 19:34:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 406 ms / 2,000 ms
コード長 3,388 bytes
コンパイル時間 1,788 ms
コンパイル使用メモリ 178,864 KB
実行使用メモリ 31,728 KB
最終ジャッジ日時 2024-04-28 10:26:54
合計ジャッジ時間 19,274 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 336 ms
18,060 KB
testcase_01 AC 334 ms
18,232 KB
testcase_02 AC 333 ms
18,260 KB
testcase_03 AC 334 ms
18,116 KB
testcase_04 AC 335 ms
18,104 KB
testcase_05 AC 403 ms
31,620 KB
testcase_06 AC 405 ms
31,576 KB
testcase_07 AC 406 ms
31,728 KB
testcase_08 AC 278 ms
12,200 KB
testcase_09 AC 293 ms
15,532 KB
testcase_10 AC 388 ms
25,952 KB
testcase_11 AC 392 ms
31,672 KB
testcase_12 AC 276 ms
13,168 KB
testcase_13 AC 176 ms
6,940 KB
testcase_14 AC 322 ms
17,412 KB
testcase_15 AC 290 ms
14,204 KB
testcase_16 AC 167 ms
6,944 KB
testcase_17 AC 228 ms
11,432 KB
testcase_18 AC 184 ms
11,556 KB
testcase_19 AC 241 ms
8,704 KB
testcase_20 AC 334 ms
17,784 KB
testcase_21 AC 32 ms
6,944 KB
testcase_22 AC 222 ms
11,624 KB
testcase_23 AC 242 ms
13,012 KB
testcase_24 AC 165 ms
10,684 KB
testcase_25 AC 332 ms
17,624 KB
testcase_26 AC 266 ms
13,808 KB
testcase_27 AC 312 ms
15,932 KB
testcase_28 AC 301 ms
15,612 KB
testcase_29 AC 292 ms
15,804 KB
testcase_30 AC 157 ms
8,260 KB
testcase_31 AC 152 ms
6,940 KB
testcase_32 AC 130 ms
8,612 KB
testcase_33 AC 142 ms
8,456 KB
testcase_34 AC 281 ms
14,044 KB
testcase_35 AC 32 ms
6,944 KB
testcase_36 AC 278 ms
15,264 KB
testcase_37 AC 249 ms
12,132 KB
testcase_38 AC 67 ms
6,940 KB
testcase_39 AC 145 ms
9,512 KB
testcase_40 AC 127 ms
6,940 KB
testcase_41 AC 215 ms
9,024 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 2 ms
6,940 KB
testcase_44 AC 2 ms
6,940 KB
testcase_45 AC 306 ms
18,096 KB
testcase_46 AC 306 ms
18,092 KB
testcase_47 AC 303 ms
18,748 KB
testcase_48 AC 303 ms
18,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.08.31 19:34:51
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;

template< int MOD >
struct mint {
public:
    long long x;
    mint(long long x = 0) :x((x%MOD+MOD)%MOD) {}
    mint(std::string &s) {
        long long z = 0;
        for (int i = 0; i < s.size(); i++) {
            z *= 10;
            z += s[i] - '0';
            z %= MOD;
        }
        this->x = z;
    }
    mint& operator+=(const mint &a) {
        if ((x += a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator-=(const mint &a) {
        if ((x += MOD - a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator*=(const mint &a) {
        (x *= a.x) %= MOD;
        return *this;
    }
    mint& operator/=(const mint &a) {
        long long n = MOD - 2;
        mint u = 1, b = a;
        while (n > 0) {
            if (n & 1) {
                u *= b;
            }
            b *= b;
            n >>= 1;
        }
        return *this *= u;
    }
    mint operator+(const mint &a) const {
        mint res(*this);
        return res += a;
    }
    mint operator-() const {return mint() -= *this; }
    mint operator-(const mint &a) const {
        mint res(*this);
        return res -= a;
    }
    mint operator*(const mint &a) const {
        mint res(*this);
        return res *= a;
    }
    mint operator/(const mint &a) const {
        mint res(*this);
        return res /= a;
    }
    friend std::ostream& operator<<(std::ostream &os, const mint &n) {
        return os << n.x;
    }
    friend std::istream &operator>>(std::istream &is, mint &n) {
        long long x;
        is >> x;
        n = mint(x);
        return is;
    }
    bool operator==(const mint &a) const {
        return this->x == a.x;
    }
    mint pow(long long k) const {
        mint ret = 1;
        mint p = this->x;
        while (k > 0) {
            if (k & 1) {
                ret *= p;
            }
            p *= p;
            k >>= 1;
        }
        return ret;
    }
};

class UnionFind {
private:
    vector<int> par;
public:
    UnionFind(int n) {
        par.resize(n, -1);
    }
    int root(int x) {
        if (par[x] < 0) return x;
        return par[x] = root(par[x]);
    }
    bool unite(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) return false;
        if (size(rx) < size(ry)) swap(rx, ry);
        par[rx] += par[ry];
        par[ry] = rx;
        return true;
    }
    bool same(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
    int size(int x) {
        return -par[root(x)];
    }
};
constexpr int MOD = 1e9 + 7;
int n,m;
mint<MOD> x,ans;
vector<vector<pair<int,mint<MOD>>>> g;
int dfs(int v, int p = -1, mint<MOD> cost = 0) {
    int cnt = 1;
    for(auto e : g[v]) {
        if (e.first == p) continue;
        cnt += dfs(e.first,v, e.second);
    }
    ans += cost * cnt * (n - cnt);
    return cnt;
}
int main() {
    cin >> n >> m >> x;
    g.resize(n);
    UnionFind uf(n);
    for (int i = 0; i < m; i++) {
        int a,b,c;cin >> a >> b >> c;
        a--;b--;
        if (uf.same(a,b)) continue;
        uf.unite(a,b);
        g[a].push_back({b,x.pow(c)});
        g[b].push_back({a,x.pow(c)});
    }
    dfs(0);
    cout << ans << endl;
    return 0;
}
0