結果

問題 No.1207 グラフX
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-01-23 01:32:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 446 ms / 2,000 ms
コード長 2,558 bytes
コンパイル時間 1,431 ms
コンパイル使用メモリ 122,676 KB
実行使用メモリ 41,364 KB
最終ジャッジ日時 2023-09-07 08:10:24
合計ジャッジ時間 23,081 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 361 ms
26,548 KB
testcase_01 AC 359 ms
26,676 KB
testcase_02 AC 353 ms
26,456 KB
testcase_03 AC 356 ms
26,556 KB
testcase_04 AC 360 ms
26,764 KB
testcase_05 AC 445 ms
40,196 KB
testcase_06 AC 440 ms
39,884 KB
testcase_07 AC 445 ms
41,364 KB
testcase_08 AC 308 ms
20,612 KB
testcase_09 AC 314 ms
23,076 KB
testcase_10 AC 438 ms
34,256 KB
testcase_11 AC 446 ms
40,576 KB
testcase_12 AC 298 ms
19,836 KB
testcase_13 AC 191 ms
11,728 KB
testcase_14 AC 361 ms
26,752 KB
testcase_15 AC 317 ms
22,068 KB
testcase_16 AC 179 ms
12,428 KB
testcase_17 AC 242 ms
18,296 KB
testcase_18 AC 191 ms
16,352 KB
testcase_19 AC 266 ms
14,748 KB
testcase_20 AC 355 ms
26,740 KB
testcase_21 AC 35 ms
4,784 KB
testcase_22 AC 238 ms
19,124 KB
testcase_23 AC 257 ms
18,988 KB
testcase_24 AC 166 ms
14,760 KB
testcase_25 AC 350 ms
27,244 KB
testcase_26 AC 277 ms
21,196 KB
testcase_27 AC 335 ms
25,136 KB
testcase_28 AC 321 ms
22,984 KB
testcase_29 AC 307 ms
26,012 KB
testcase_30 AC 167 ms
12,140 KB
testcase_31 AC 167 ms
10,396 KB
testcase_32 AC 126 ms
11,892 KB
testcase_33 AC 141 ms
12,076 KB
testcase_34 AC 298 ms
21,504 KB
testcase_35 AC 35 ms
4,784 KB
testcase_36 AC 294 ms
22,360 KB
testcase_37 AC 270 ms
19,716 KB
testcase_38 AC 68 ms
7,200 KB
testcase_39 AC 142 ms
13,648 KB
testcase_40 AC 138 ms
10,024 KB
testcase_41 AC 234 ms
15,832 KB
testcase_42 AC 1 ms
4,384 KB
testcase_43 AC 1 ms
4,384 KB
testcase_44 AC 1 ms
4,384 KB
testcase_45 AC 335 ms
26,688 KB
testcase_46 AC 330 ms
26,580 KB
testcase_47 AC 333 ms
26,508 KB
testcase_48 AC 327 ms
27,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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