結果

問題 No.1207 グラフX
ユーザー m_tsubasam_tsubasa
提出日時 2020-08-30 13:26:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 378 ms / 2,000 ms
コード長 3,248 bytes
コンパイル時間 2,232 ms
コンパイル使用メモリ 211,276 KB
実行使用メモリ 31,476 KB
最終ジャッジ日時 2024-04-27 06:53:29
合計ジャッジ時間 16,175 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 294 ms
17,336 KB
testcase_01 AC 293 ms
17,340 KB
testcase_02 AC 290 ms
17,232 KB
testcase_03 AC 296 ms
17,468 KB
testcase_04 AC 299 ms
17,332 KB
testcase_05 AC 378 ms
31,352 KB
testcase_06 AC 348 ms
31,420 KB
testcase_07 AC 346 ms
31,440 KB
testcase_08 AC 261 ms
11,404 KB
testcase_09 AC 264 ms
13,864 KB
testcase_10 AC 327 ms
24,224 KB
testcase_11 AC 340 ms
31,476 KB
testcase_12 AC 247 ms
12,472 KB
testcase_13 AC 167 ms
6,940 KB
testcase_14 AC 284 ms
16,720 KB
testcase_15 AC 260 ms
13,488 KB
testcase_16 AC 159 ms
6,944 KB
testcase_17 AC 206 ms
10,868 KB
testcase_18 AC 165 ms
11,212 KB
testcase_19 AC 225 ms
8,192 KB
testcase_20 AC 296 ms
16,976 KB
testcase_21 AC 32 ms
6,944 KB
testcase_22 AC 194 ms
10,980 KB
testcase_23 AC 214 ms
12,384 KB
testcase_24 AC 137 ms
10,420 KB
testcase_25 AC 282 ms
16,836 KB
testcase_26 AC 225 ms
13,188 KB
testcase_27 AC 276 ms
15,300 KB
testcase_28 AC 263 ms
14,040 KB
testcase_29 AC 263 ms
15,116 KB
testcase_30 AC 145 ms
7,808 KB
testcase_31 AC 147 ms
6,940 KB
testcase_32 AC 115 ms
8,448 KB
testcase_33 AC 128 ms
8,064 KB
testcase_34 AC 251 ms
13,348 KB
testcase_35 AC 31 ms
6,940 KB
testcase_36 AC 251 ms
13,904 KB
testcase_37 AC 238 ms
11,432 KB
testcase_38 AC 62 ms
6,944 KB
testcase_39 AC 129 ms
8,708 KB
testcase_40 AC 128 ms
6,940 KB
testcase_41 AC 202 ms
8,192 KB
testcase_42 AC 1 ms
6,940 KB
testcase_43 AC 2 ms
6,940 KB
testcase_44 AC 2 ms
6,940 KB
testcase_45 AC 274 ms
17,460 KB
testcase_46 AC 273 ms
17,332 KB
testcase_47 AC 278 ms
17,332 KB
testcase_48 AC 275 ms
17,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct Unionfind {
  // tree number
  vector<int> par;
  // constructor
  Unionfind(int n = 1) : par(n, -1) {}
  // search root
  int root(int x) {
    if (par[x] < 0) return x;
    return par[x] = root(par[x]);
  }
  // is same?
  bool issame(int x, int y) { return root(x) == root(y); }

  // add
  // already added, return 0
  bool uni(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return 0;
    if (par[x] > par[y]) swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return 1;
  }
  int size(int x) { return -par[root(x)]; }
};

template <int mod = (int)(1e9 + 7)>
struct ModInt {
  int x;
  constexpr ModInt() : x(0) {}
  constexpr ModInt(int64_t y)
      : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
  constexpr ModInt &operator+=(const ModInt &p) noexcept {
    if ((x += p.x) >= mod) x -= mod;
    return *this;
  }
  constexpr ModInt &operator-=(const ModInt &p) noexcept {
    if ((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
  constexpr ModInt &operator*=(const ModInt &p) noexcept {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  constexpr ModInt &operator/=(const ModInt &p) noexcept {
    *this *= p.inverse();
    return *this;
  }
  constexpr ModInt operator-() const { return ModInt(-x); }
  constexpr ModInt operator+(const ModInt &p) const noexcept {
    return ModInt(*this) += p;
  }
  constexpr ModInt operator-(const ModInt &p) const noexcept {
    return ModInt(*this) -= p;
  }
  constexpr ModInt operator*(const ModInt &p) const noexcept {
    return ModInt(*this) *= p;
  }
  constexpr ModInt operator/(const ModInt &p) const noexcept {
    return ModInt(*this) /= p;
  }
  constexpr bool operator==(const ModInt &p) const noexcept { return x == p.x; }
  constexpr bool operator!=(const ModInt &p) const noexcept { return x != p.x; }
  constexpr ModInt inverse() const noexcept {
    int a = x, b = mod, u = 1, v = 0, t = 0;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
  constexpr ModInt pow(int64_t n) const {
    ModInt res(1), mul(x);
    while (n) {
      if (n & 1) res *= mul;
      mul *= mul;
      n >>= 1;
    }
    return res;
  }
  friend constexpr ostream &operator<<(ostream &os, const ModInt &p) noexcept {
    return os << p.x;
  }
  friend constexpr istream &operator>>(istream &is, ModInt &a) noexcept {
    int64_t t = 0;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }
  constexpr int get_mod() { return mod; }
};

using P = pair<int, int>;

int n, m, x;
Unionfind uf;
vector<int> dp;
vector<vector<P>> g;

ModInt<> solve(int now, int par);

int main() {
  cin >> n >> m >> x;
  uf = Unionfind(n);
  g.resize(n);
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    if (!uf.issame(--a, --b)) {
      g[a].push_back(P(b, c));
      g[b].push_back(P(a, c));
      uf.uni(a, b);
    }
  }
  dp.assign(n, 1);
  cout << solve(0, -1) << endl;
  return 0;
}

ModInt<> solve(int now, int par) {
  ModInt<> res;
  for (auto [to, z] : g[now])
    if (to != par) {
      res += solve(to, now);
      res += ModInt<>(x).pow(z) * dp[to] * (n - dp[to]);
      dp[now] += dp[to];
    }
  return res;
}
0