結果

問題 No.1207 グラフX
ユーザー risujirohrisujiroh
提出日時 2020-08-30 13:14:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 249 ms / 2,000 ms
コード長 4,042 bytes
コンパイル時間 3,001 ms
コンパイル使用メモリ 274,220 KB
実行使用メモリ 44,024 KB
最終ジャッジ日時 2024-04-27 06:46:45
合計ジャッジ時間 12,754 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 201 ms
29,840 KB
testcase_01 AC 212 ms
29,700 KB
testcase_02 AC 217 ms
29,308 KB
testcase_03 AC 210 ms
29,312 KB
testcase_04 AC 213 ms
29,184 KB
testcase_05 AC 249 ms
43,392 KB
testcase_06 AC 242 ms
43,408 KB
testcase_07 AC 242 ms
44,024 KB
testcase_08 AC 157 ms
18,596 KB
testcase_09 AC 170 ms
23,736 KB
testcase_10 AC 235 ms
36,084 KB
testcase_11 AC 245 ms
43,392 KB
testcase_12 AC 156 ms
20,608 KB
testcase_13 AC 68 ms
7,368 KB
testcase_14 AC 204 ms
28,144 KB
testcase_15 AC 171 ms
22,684 KB
testcase_16 AC 71 ms
8,768 KB
testcase_17 AC 130 ms
17,468 KB
testcase_18 AC 111 ms
17,952 KB
testcase_19 AC 112 ms
12,544 KB
testcase_20 AC 201 ms
28,768 KB
testcase_21 AC 12 ms
6,940 KB
testcase_22 AC 126 ms
17,800 KB
testcase_23 AC 143 ms
20,548 KB
testcase_24 AC 95 ms
16,280 KB
testcase_25 AC 202 ms
28,492 KB
testcase_26 AC 151 ms
23,068 KB
testcase_27 AC 185 ms
25,592 KB
testcase_28 AC 165 ms
23,940 KB
testcase_29 AC 171 ms
25,580 KB
testcase_30 AC 81 ms
12,028 KB
testcase_31 AC 58 ms
6,940 KB
testcase_32 AC 71 ms
12,732 KB
testcase_33 AC 74 ms
12,348 KB
testcase_34 AC 161 ms
22,236 KB
testcase_35 AC 13 ms
6,944 KB
testcase_36 AC 159 ms
23,544 KB
testcase_37 AC 136 ms
18,508 KB
testcase_38 AC 32 ms
6,980 KB
testcase_39 AC 78 ms
13,584 KB
testcase_40 AC 42 ms
6,944 KB
testcase_41 AC 102 ms
12,692 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 184 ms
29,312 KB
testcase_46 AC 193 ms
29,436 KB
testcase_47 AC 193 ms
29,160 KB
testcase_48 AC 188 ms
29,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/extc++.h>

#ifndef DUMP
#define DUMP(...) void(0)
#endif

using namespace std;

template <class T, class Op = multiplies<>>
constexpr T power(T a, uint64_t n, T init = 1, Op op = Op{}) {
  while (n) {
    if (n & 1) init = op(init, a);
    if (n >>= 1) a = op(a, a);
  }
  return init;
}

template <uint32_t Mod>
struct modular {
  using T = modular;
  static constexpr uint32_t mod = Mod;
  uint32_t v;
  modular(int64_t x = 0) : v((x %= mod) < 0 ? x + mod : x) {}
  T operator-() const { return T() - *this; }
  T& operator+=(T b) { return v += int(v += b.v - mod) < 0 ? mod : 0, *this; }
  T& operator-=(T b) { return v += int(v -= b.v) < 0 ? mod : 0, *this; }
  T& operator*=(T b) { return v = uint64_t(v) * b.v % mod, *this; }
  T& operator/=(T b) { return *this *= power(b, mod - 2); }
  friend T operator+(T a, T b) { return a += b; }
  friend T operator-(T a, T b) { return a -= b; }
  friend T operator*(T a, T b) { return a *= b; }
  friend T operator/(T a, T b) { return a /= b; }
  friend bool operator==(T a, T b) { return a.v == b.v; }
};

struct dsu {
  int cc;
  vector<int> p, sz;
  dsu(int n = 0) : cc(n), p(n, -1), sz(n, 1) {}
  int root(int v) {
    if (p[v] == -1) return v;
    return p[v] = root(p[v]);
  }
  bool unite(int u, int v) {
    u = root(u), v = root(v);
    if (u == v) return false;
    --cc;
    if (sz[u] < sz[v]) swap(u, v);
    p[v] = u;
    sz[u] += sz[v];
    return true;
  }
  bool same(int u, int v) { return root(u) == root(v); }
  int size(int v) { return sz[root(v)]; }
};

struct graph {
  struct edge {
    int src, dst, cost;
    int operator-(int v) const { return src ^ dst ^ v; }
  };
  int n, m;
  vector<edge> edges;
  vector<vector<pair<int, int>>> adj;
  graph(int _n = 0) : n(_n), m(0), adj(n) {}
  int add(const edge& e, bool directed = false) {
    edges.push_back(e);
    adj[e.src].emplace_back(m, e.dst);
    if (not directed) adj[e.dst].emplace_back(m, e.src);
    return m++;
  }
};

struct dfs_forest : graph {
  using T = decltype(edge::cost);
  vector<int> root, pv, pe, sz, dep, min_dep, last, ord, in, out;
  vector<T> dist;
  int trials;
  dfs_forest(int _n = 0) : graph(_n), dist(n), trials(0) {
    for (auto p : {&root, &pv, &pe, &sz, &dep, &min_dep, &last, &in, &out})
      p->assign(n, -1);
  }
  int add(const edge& e) { return graph::add(e); }
  void dfs(int v) {
    sz[v] = 1, min_dep[v] = dep[v], last[v] = trials;
    in[v] = size(ord), ord.push_back(v);
    for (auto [id, u] : adj[v]) {
      if (id == pe[v]) continue;
      if (last[u] == trials) {
        min_dep[v] = min(min_dep[v], dep[u]);
        continue;
      }
      root[u] = root[v], pv[u] = v, pe[u] = id, dep[u] = dep[v] + 1;
      dist[u] = dist[v] + edges[id].cost;
      dfs(u);
      sz[v] += sz[u], min_dep[v] = min(min_dep[v], min_dep[u]);
    }
    out[v] = size(ord);
  }
  void build(int r, bool clear_ord = true) {
    root[r] = r, pv[r] = pe[r] = -1, dep[r] = 0, dist[r] = T{};
    if (clear_ord) ord.clear();
    dfs(r);
    ++trials;
  }
  void build() {
    fill(begin(root), end(root), -1);
    for (int v = 0; v < n; ++v)
      if (root[v] == -1) build(v, v == 0);
  }
  int farther(int id) const {
    int u = edges[id].src, v = edges[id].dst;
    return dep[u] < dep[v] ? v : u;
  }
  bool spans(int id) const { return id == pe[farther(id)]; }
  bool anc(int u, int v) const { return in[u] <= in[v] and out[v] <= out[u]; }
};

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int n, m, base;
  cin >> n >> m >> base;
  dsu d(n);
  dfs_forest g(n);
  while (m--) {
    int u, v, z;
    cin >> u >> v >> z;
    --u, --v;
    if (d.same(u, v)) continue;
    d.unite(u, v);
    g.add({u, v, z});
  }
  g.build();

  using mint = modular<power(10, 9) + 7>;
  mint res;
  for (int v = 1; v < n; ++v) {
    res += power<mint>(base, g.edges[g.pe[v]].cost) * g.sz[v] * (n - g.sz[v]);
  }
  cout << res.v << '\n';
}
0