結果

問題 No.1207 グラフX
ユーザー simansiman
提出日時 2023-03-15 20:26:22
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,642 bytes
コンパイル時間 1,629 ms
コンパイル使用メモリ 133,140 KB
実行使用メモリ 15,648 KB
最終ジャッジ日時 2023-10-18 12:30:38
合計ジャッジ時間 26,388 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 AC 192 ms
10,304 KB
testcase_14 RE -
testcase_15 RE -
testcase_16 AC 172 ms
11,088 KB
testcase_17 RE -
testcase_18 RE -
testcase_19 AC 250 ms
14,268 KB
testcase_20 RE -
testcase_21 AC 38 ms
6,932 KB
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 AC 158 ms
12,976 KB
testcase_31 AC 173 ms
10,072 KB
testcase_32 AC 123 ms
12,712 KB
testcase_33 AC 135 ms
12,976 KB
testcase_34 RE -
testcase_35 AC 39 ms
6,928 KB
testcase_36 RE -
testcase_37 RE -
testcase_38 AC 67 ms
8,536 KB
testcase_39 AC 138 ms
13,272 KB
testcase_40 AC 148 ms
10,104 KB
testcase_41 AC 220 ms
14,020 KB
testcase_42 AC 3 ms
6,228 KB
testcase_43 AC 3 ms
6,232 KB
testcase_44 AC 2 ms
6,232 KB
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 RE -
権限があれば一括ダウンロードができます

ソースコード

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;
  }
};

vector<int> G[100010];
int counter[100010];

int dfs(int v, bool *visited) {
  visited[v] = true;
  int cnt = 1;

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

    cnt += dfs(u, visited);
  }

  visited[v] = false;
  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);
  }

  bool visited[N + 1];
  memset(visited, false, sizeof(visited));
  dfs(1, visited);

  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