結果

問題 No.1207 グラフX
ユーザー simansiman
提出日時 2023-03-15 21:19:13
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 2,542 bytes
コンパイル時間 1,954 ms
コンパイル使用メモリ 133,444 KB
実行使用メモリ 814,348 KB
最終ジャッジ日時 2023-10-18 12:33:13
合計ジャッジ時間 8,958 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

const ll MOD = 1000000007;

ll mod_pow(ll x, ll n, ll mod = MOD) {
  ll res = 1;

  while (n > 0) {
    if (n & 1) {
      res = res * x % mod;
    }

    x = x * x % mod;
    n >>= 1;
  }

  return res;
}

class UnionFind {
public:
  vector<int> _parent;
  vector<int> _rank;
  vector<int> _size;

  UnionFind(int n) {
    for (int i = 0; i < n; ++i) {
      _parent.push_back(i);
      _rank.push_back(0);
      _size.push_back(1);
    }
  }

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

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;

    if (_rank[x] < _rank[y]) {
      _parent[x] = y;
      _size[y] += _size[x];
    } else {
      _parent[y] = x;
      _size[x] += _size[y];
      if (_rank[x] == _rank[y]) ++_rank[x];
    }
  }

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

  int size(int x) {
    return _size[find(x)];
  }
};

struct Edge {
  int x;
  int y;
  ll z;

  Edge(int x = -1, int y = -1, ll z = -1) {
    this->x = x;
    this->y = y;
    this->z = z;
  }

  bool operator>(const Edge &e) const {
    return z > e.z;
  }
};

const int MAX_N = 200020;

vector<int> G[MAX_N];
ll counter[MAX_N];

int dfs(int v, int parent = -1) {
  int cnt = 1;

  for (int u : G[v]) {
    if (u == parent) continue;

    cnt += dfs(u);
  }

  counter[v] = cnt;

  return cnt;
}

int main() {
  memset(counter, 0, sizeof(counter));

  ll N, M, X;
  cin >> N >> M >> X;

  priority_queue <Edge, vector<Edge>, greater<Edge>> pque;
  for (int i = 0; i < M; ++i) {
    ll x, y, z;
    cin >> x >> y >> z;

    pque.push(Edge(x, y, z));
  }

  UnionFind uf(N + 1);
  vector<Edge> edges;

  while (not pque.empty()) {
    Edge e = pque.top();
    pque.pop();

    if (uf.same(e.x, e.y)) continue;
    uf.unite(e.x, e.y);

    edges.push_back(e);
    G[e.x].push_back(e.y);
    G[e.y].push_back(e.x);
  }

  dfs(1);

  ll ans = 0;

  for (Edge &e : edges) {
    if (counter[e.x] < counter[e.y]) {
      ll a = counter[e.x];
      ll b = N - a;
      ans += mod_pow(X, e.z) * a * b % MOD;
    } else {
      ll a = counter[e.y];
      ll b = N - a;
      ans += mod_pow(X, e.z) * a * b % MOD;
    }

    ans %= MOD;
  }

  cout << ans << endl;

  return 0;
}
0