結果

問題 No.1207 グラフX
ユーザー outlineoutline
提出日時 2020-12-11 00:08:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 256 ms / 2,000 ms
コード長 3,967 bytes
コンパイル時間 2,953 ms
コンパイル使用メモリ 154,260 KB
実行使用メモリ 26,912 KB
最終ジャッジ日時 2023-10-20 01:04:57
合計ジャッジ時間 16,231 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 193 ms
20,472 KB
testcase_01 AC 203 ms
20,476 KB
testcase_02 AC 195 ms
20,496 KB
testcase_03 AC 195 ms
20,480 KB
testcase_04 AC 195 ms
20,472 KB
testcase_05 AC 256 ms
26,912 KB
testcase_06 AC 250 ms
26,912 KB
testcase_07 AC 248 ms
26,912 KB
testcase_08 AC 144 ms
14,216 KB
testcase_09 AC 159 ms
16,540 KB
testcase_10 AC 247 ms
23,216 KB
testcase_11 AC 249 ms
26,912 KB
testcase_12 AC 151 ms
14,820 KB
testcase_13 AC 72 ms
7,596 KB
testcase_14 AC 188 ms
19,504 KB
testcase_15 AC 168 ms
16,256 KB
testcase_16 AC 74 ms
7,964 KB
testcase_17 AC 122 ms
12,872 KB
testcase_18 AC 100 ms
12,920 KB
testcase_19 AC 116 ms
10,544 KB
testcase_20 AC 194 ms
20,088 KB
testcase_21 AC 13 ms
4,348 KB
testcase_22 AC 123 ms
13,108 KB
testcase_23 AC 134 ms
14,544 KB
testcase_24 AC 87 ms
11,832 KB
testcase_25 AC 206 ms
19,760 KB
testcase_26 AC 148 ms
15,532 KB
testcase_27 AC 178 ms
17,980 KB
testcase_28 AC 164 ms
16,632 KB
testcase_29 AC 168 ms
17,796 KB
testcase_30 AC 78 ms
9,332 KB
testcase_31 AC 60 ms
6,640 KB
testcase_32 AC 69 ms
9,420 KB
testcase_33 AC 72 ms
9,380 KB
testcase_34 AC 155 ms
16,032 KB
testcase_35 AC 13 ms
4,348 KB
testcase_36 AC 156 ms
16,472 KB
testcase_37 AC 138 ms
13,900 KB
testcase_38 AC 33 ms
6,128 KB
testcase_39 AC 80 ms
10,048 KB
testcase_40 AC 47 ms
5,264 KB
testcase_41 AC 106 ms
10,492 KB
testcase_42 AC 2 ms
4,348 KB
testcase_43 AC 2 ms
4,348 KB
testcase_44 AC 2 ms
4,348 KB
testcase_45 AC 191 ms
20,464 KB
testcase_46 AC 193 ms
20,464 KB
testcase_47 AC 190 ms
20,464 KB
testcase_48 AC 191 ms
20,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

struct mint {
    int x;
    mint() : x(0) {}
    mint(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
    mint& operator+=(const mint& p){
        if((x += p.x) >= mod)   x -= mod;
        return *this;
    }
    mint& operator-=(const mint& p){
        if((x -= p.x) < 0)  x += mod;
        return *this;
    }
    mint& operator*=(const mint& p){
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }
    mint& operator/=(const mint& p){
        *this *= p.inverse();
        return *this;
    }
    mint operator-() const { return mint(-x); }
    mint operator+(const mint& p) const { return mint(*this) += p; }
    mint operator-(const mint& p) const { return mint(*this) -= p; }
    mint operator*(const mint& p) const { return mint(*this) *= p; }
    mint operator/(const mint& p) const { return mint(*this) /= p; }
    bool operator==(const mint& p) const { return x == p.x; }
    bool operator!=(const mint& p) const { return x != p.x; }
    mint pow(int64_t n) const {
        mint res = 1, mul = x;
        while(n > 0){
            if(n & 1)   res *= mul;
            mul *= mul;
            n >>= 1;
        }
        return res;
    }
    mint inverse() const { return pow(mod - 2); }
    friend ostream& operator<<(ostream& os, const mint& p){
        return os << p.x;
    }
    friend istream& operator>>(istream& is, mint& p){
        int64_t val;
        is >> val;
        p = mint(val);
        return is;
    }
};

struct UnionFind{
    int sz;     // vertex number
    vector<int> par;
    vector<int> rank;

    UnionFind(int n) : sz(n) {
        par.resize(sz);
        rank.assign(sz, 0);
        for(int i = 0; i < sz; ++i){
            par[i] = i;
        }
    }

    int find(int x){
        if(par[x] == x) return x;
        else    return par[x] = find(par[x]);
    }

    bool same(int x, int y){
        return find(x) == find(y);
    }

    void unite(int x, int y){
        x = find(x), y = find(y);
        if(x == y)  return;
        if(rank[x] < rank[y])   par[x] = y;
        else{
            par[y] = x;
            if(rank[x] == rank[y])  ++rank[x];
        }
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M, X;
    cin >> N >> M >> X;
    vector<tuple<int, int, int>> es(M);
    for(auto& [z, x, y] : es){
        cin >> x >> y >> z;
        --x, --y;
    }
    sort(es.begin(), es.end());

    vector<vector<pair<int, mint>>> graph(N);
    UnionFind uf(N);
    for(auto& [z, x, y] : es){
        if(uf.same(x, y))   continue;
        uf.unite(x, y);
        graph[x].emplace_back(y, mint(X).pow(z));
        graph[y].emplace_back(x, mint(X).pow(z));
    }
    
    vector<int> dp(N, 1);
    mint ans = 0;
    auto dfs = [&](auto&& self, int from = 0, int par = -1) -> void {
        for(auto [to, cost] : graph[from]){
            if(to == par)   continue;
            self(self, to, from);
            dp[from] += dp[to];
            ans += cost * dp[to] * (N - dp[to]);
        }
    };
    dfs(dfs);
    
    cout << ans << endl;

    return 0;
}
0