結果

問題 No.1207 グラフX
ユーザー poohbearpoohbear
提出日時 2020-08-30 14:01:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 383 ms / 2,000 ms
コード長 4,674 bytes
コンパイル時間 2,537 ms
コンパイル使用メモリ 220,088 KB
実行使用メモリ 45,528 KB
最終ジャッジ日時 2024-04-27 07:14:36
合計ジャッジ時間 16,373 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 308 ms
30,328 KB
testcase_01 AC 312 ms
29,016 KB
testcase_02 AC 300 ms
29,176 KB
testcase_03 AC 300 ms
29,212 KB
testcase_04 AC 319 ms
29,060 KB
testcase_05 AC 383 ms
45,108 KB
testcase_06 AC 378 ms
45,460 KB
testcase_07 AC 375 ms
44,936 KB
testcase_08 AC 258 ms
19,188 KB
testcase_09 AC 273 ms
27,068 KB
testcase_10 AC 374 ms
39,072 KB
testcase_11 AC 376 ms
45,528 KB
testcase_12 AC 253 ms
24,112 KB
testcase_13 AC 173 ms
7,584 KB
testcase_14 AC 302 ms
28,820 KB
testcase_15 AC 278 ms
26,000 KB
testcase_16 AC 162 ms
8,984 KB
testcase_17 AC 217 ms
17,868 KB
testcase_18 AC 172 ms
17,996 KB
testcase_19 AC 233 ms
13,876 KB
testcase_20 AC 311 ms
28,616 KB
testcase_21 AC 32 ms
6,944 KB
testcase_22 AC 211 ms
17,900 KB
testcase_23 AC 232 ms
24,980 KB
testcase_24 AC 153 ms
16,276 KB
testcase_25 AC 302 ms
28,464 KB
testcase_26 AC 244 ms
24,608 KB
testcase_27 AC 293 ms
27,036 KB
testcase_28 AC 279 ms
25,868 KB
testcase_29 AC 275 ms
27,628 KB
testcase_30 AC 152 ms
13,504 KB
testcase_31 AC 153 ms
6,940 KB
testcase_32 AC 122 ms
13,712 KB
testcase_33 AC 134 ms
13,560 KB
testcase_34 AC 263 ms
25,148 KB
testcase_35 AC 32 ms
6,944 KB
testcase_36 AC 259 ms
25,984 KB
testcase_37 AC 235 ms
18,580 KB
testcase_38 AC 65 ms
7,252 KB
testcase_39 AC 133 ms
14,376 KB
testcase_40 AC 125 ms
6,944 KB
testcase_41 AC 211 ms
13,856 KB
testcase_42 AC 1 ms
6,944 KB
testcase_43 AC 1 ms
6,940 KB
testcase_44 AC 2 ms
6,940 KB
testcase_45 AC 281 ms
29,400 KB
testcase_46 AC 283 ms
29,180 KB
testcase_47 AC 277 ms
29,124 KB
testcase_48 AC 283 ms
29,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(a,n) for (int a = 0; a < (n); ++a)
using namespace std;
using ll = long long;
typedef pair<ll,ll> P;
typedef pair<P,ll> PP;
typedef vector<vector<ll> > Graph;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll INF = 1e18;

// auto mod int
// depends on Template

const int mod = 1000000007;
//const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  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) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

long long modpow(long long a, long long n) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

//UnionFind
struct UnionFind {
    vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2

    UnionFind(int n) : par(n, -1) { }

    int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
        if (par[x] < 0) return x;
        return par[x] = root(par[x]);
    }

    bool merge(int x, int y) {//xの木とyの木を結合する
        x = root(x); y = root(y);
        if (x == y) return false;
        if (par[x] > par[y]) swap(x, y); // merge technique
        par[x] += par[y];
        par[y] = x;
        return true;
    }

    int size(int x) {//sizeの取得
        return -par[root(x)];
    }

    bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
};

template< typename T = int >
struct Edge {
  int from, to;
  T cost;
  int idx;

  Edge() = default;

  Edge(int from, int to, T cost = 1, int idx = -1) : from(from), to(to), cost(cost), idx(idx) {}

  operator int() const { return to; }
};

template<typename T >
struct TreeDP{
    using FX = function<T(T,T)>;//T●T->Tとなる関数の型
    using FA = function<T(T)>;
    const T e;//単位元
    struct Edge{
        int to;
        ll cost;
    };
    using Graph = vector<vector<Edge> >;

    FX merge;
    FA add_root;
    Graph G;
    int n;//木の要素数
    vector< T > dp;

    TreeDP(FX merge_,FA add_root_, int n_, T e_):merge(merge_),add_root(add_root_),n(n_),e(e_){
        dp.resize(n);
        G.resize(n);
    }

    void add_edge(int a,int b){
        G[a].push_back({b});
    }

    void build(){
        dfs(0);
    }

    void dfs(int v,int p=-1){
        T dp_cum = e;
        int deg = G[v].size();
        for(int i = 0; i < deg; i++){
            int x = G[v][i].to;
            if(x==p)continue;
            dfs(x,v);
            dp_cum = merge(dp_cum,dp[x]);
        }
        dp[v] = add_root(dp_cum);
    }
};

//input
ll N,M,X;


int main(){
    cin >> N >> M >> X;
    UnionFind u(N);
    Graph g(N);
    vector<Edge<ll> > edges;
    auto merge = [](ll dp_cum,ll d)->ll{
        return dp_cum+d;
    };
    auto add_root = [](ll d) -> ll{
        return d+1;
    };
    TreeDP<ll>tdp(merge,add_root,N,0);
    rep(i,M){
        int x,y;
        ll z;
        cin >> x >> y >> z;
        x--;y--;
        if(u.same(x,y)){
            continue;
        }
        u.merge(x,y);
        tdp.add_edge(x,y);
        tdp.add_edge(y,x);
        Edge<ll> e = {x,y,z};
        edges.push_back(e);
    }
    tdp.build();
    mint ans = 0;
    rep(i,edges.size()){
        int x = edges[i].from;
        int y = edges[i].to;
        ll z = edges[i].cost;
        ll ch = min(tdp.dp[x],tdp.dp[y]);
        mint tmp = 1;
        tmp *= ch;
        tmp *= N-ch;
        tmp *= modpow(X,z);
        ans += tmp;
    }
    cout << ans << endl;
    return 0;
}
0