結果

問題 No.1207 グラフX
ユーザー 0w10w1
提出日時 2020-12-19 13:43:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 260 ms / 2,000 ms
コード長 1,686 bytes
コンパイル時間 2,546 ms
コンパイル使用メモリ 221,940 KB
実行使用メモリ 35,968 KB
最終ジャッジ日時 2023-10-21 09:10:22
合計ジャッジ時間 17,990 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 199 ms
19,004 KB
testcase_01 AC 197 ms
19,008 KB
testcase_02 AC 194 ms
19,028 KB
testcase_03 AC 196 ms
19,012 KB
testcase_04 AC 192 ms
19,004 KB
testcase_05 AC 260 ms
35,968 KB
testcase_06 AC 252 ms
35,968 KB
testcase_07 AC 258 ms
35,968 KB
testcase_08 AC 164 ms
13,324 KB
testcase_09 AC 174 ms
16,020 KB
testcase_10 AC 250 ms
27,520 KB
testcase_11 AC 254 ms
35,968 KB
testcase_12 AC 159 ms
13,884 KB
testcase_13 AC 81 ms
7,216 KB
testcase_14 AC 192 ms
18,336 KB
testcase_15 AC 166 ms
15,256 KB
testcase_16 AC 83 ms
7,744 KB
testcase_17 AC 129 ms
12,284 KB
testcase_18 AC 100 ms
12,096 KB
testcase_19 AC 129 ms
10,232 KB
testcase_20 AC 191 ms
18,636 KB
testcase_21 AC 14 ms
4,348 KB
testcase_22 AC 132 ms
12,508 KB
testcase_23 AC 145 ms
13,608 KB
testcase_24 AC 88 ms
11,132 KB
testcase_25 AC 196 ms
18,580 KB
testcase_26 AC 156 ms
14,708 KB
testcase_27 AC 180 ms
16,888 KB
testcase_28 AC 173 ms
16,120 KB
testcase_29 AC 169 ms
16,712 KB
testcase_30 AC 81 ms
9,092 KB
testcase_31 AC 68 ms
6,424 KB
testcase_32 AC 66 ms
9,156 KB
testcase_33 AC 76 ms
9,064 KB
testcase_34 AC 161 ms
15,044 KB
testcase_35 AC 15 ms
4,348 KB
testcase_36 AC 159 ms
15,708 KB
testcase_37 AC 142 ms
13,012 KB
testcase_38 AC 34 ms
5,896 KB
testcase_39 AC 78 ms
9,840 KB
testcase_40 AC 54 ms
5,368 KB
testcase_41 AC 117 ms
10,120 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 186 ms
18,996 KB
testcase_46 AC 189 ms
18,996 KB
testcase_47 AC 188 ms
18,996 KB
testcase_48 AC 185 ms
18,996 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);

  int N, M, X; { cin >> N >> M >> X; }

  vector<tuple<int, int, int>> edges(M); {
    for (int i = 0; i < M; ++i) {
      int X, Y, Z; { cin >> X >> Y >> Z; --X, --Y; }
      edges[i] = make_tuple(Z, X, Y);
    }
    sort(edges.begin(), edges.end());
  }

  const int D = 1e9 + 7;

  auto ipow = [D](int v, int p) {
    int r = 1;
    for (int i = p; i; i >>= 1) {
      if (i & 1) r = (int64_t) r * v % D;
      v = (int64_t) v * v % D;
    }
    return r;
  };

  int res = 0; {
    vector<vector<pair<int, int>>> g(N); {
      vector<int> fa(N); { iota(fa.begin(), fa.end(), 0); }
      function<int(int)> find = [&](int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); };
      function<bool(int, int)> unite = [&](int x, int y) {
        int fx = find(x);
        int fy = find(y);
        if (fx == fy) return false;
        fa[fx] = fy;
        return true;
      };

      for (auto [w, u, v] : edges) {
        if (unite(u, v)) {
          g[u].emplace_back(w, v);
          g[v].emplace_back(w, u);
        }
      }
    }

    vector<int> sz(N); {
      function<int(int, int)> dfs = [&](int u, int fa) {
        sz[u] = 1;
        for (auto [w, v] : g[u]) if (v != fa) sz[u] += dfs(v, u);
        return sz[u];
      };
      dfs(0, -1);
    }

    function<void(int, int, int)> dfs = [&](int u, int fa, int sup) {
      for (auto [w, v] : g[u]) if (v != fa) {
        int n = (int64_t) (sup + sz[u] - sz[v]) * sz[v] % D;
        (res += (int64_t) n * ipow(X, w) % D) %= D;
        dfs(v, u, sup + sz[u] - sz[v]);
      }
    };

    dfs(0, -1, 0);
  }

  cout << res << endl;
}

0