結果

問題 No.1207 グラフX
ユーザー simansiman
提出日時 2023-03-15 21:28:30
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 375 ms / 2,000 ms
コード長 2,591 bytes
コンパイル時間 1,428 ms
コンパイル使用メモリ 133,460 KB
実行使用メモリ 37,492 KB
最終ジャッジ日時 2023-10-18 12:34:30
合計ジャッジ時間 16,227 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 319 ms
24,556 KB
testcase_01 AC 322 ms
24,556 KB
testcase_02 AC 322 ms
24,556 KB
testcase_03 AC 320 ms
24,556 KB
testcase_04 AC 315 ms
24,556 KB
testcase_05 AC 368 ms
37,492 KB
testcase_06 AC 368 ms
37,492 KB
testcase_07 AC 369 ms
37,492 KB
testcase_08 AC 277 ms
19,080 KB
testcase_09 AC 279 ms
22,708 KB
testcase_10 AC 369 ms
31,948 KB
testcase_11 AC 375 ms
37,492 KB
testcase_12 AC 268 ms
22,444 KB
testcase_13 AC 187 ms
13,016 KB
testcase_14 AC 309 ms
24,292 KB
testcase_15 AC 284 ms
22,708 KB
testcase_16 AC 172 ms
13,800 KB
testcase_17 AC 217 ms
18,024 KB
testcase_18 AC 174 ms
17,572 KB
testcase_19 AC 251 ms
16,968 KB
testcase_20 AC 313 ms
24,556 KB
testcase_21 AC 38 ms
9,676 KB
testcase_22 AC 218 ms
18,024 KB
testcase_23 AC 235 ms
21,920 KB
testcase_24 AC 155 ms
16,780 KB
testcase_25 AC 313 ms
24,292 KB
testcase_26 AC 252 ms
22,184 KB
testcase_27 AC 296 ms
23,236 KB
testcase_28 AC 283 ms
22,972 KB
testcase_29 AC 279 ms
22,972 KB
testcase_30 AC 159 ms
15,724 KB
testcase_31 AC 167 ms
13,016 KB
testcase_32 AC 126 ms
15,460 KB
testcase_33 AC 138 ms
15,724 KB
testcase_34 AC 273 ms
22,448 KB
testcase_35 AC 38 ms
9,676 KB
testcase_36 AC 265 ms
22,708 KB
testcase_37 AC 241 ms
18,552 KB
testcase_38 AC 67 ms
11,252 KB
testcase_39 AC 141 ms
15,724 KB
testcase_40 AC 144 ms
13,016 KB
testcase_41 AC 224 ms
16,704 KB
testcase_42 AC 4 ms
8,928 KB
testcase_43 AC 5 ms
8,932 KB
testcase_44 AC 4 ms
8,932 KB
testcase_45 AC 300 ms
24,820 KB
testcase_46 AC 294 ms
24,820 KB
testcase_47 AC 295 ms
24,820 KB
testcase_48 AC 297 ms
24,820 KB
権限があれば一括ダウンロードができます

ソースコード

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];
int 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, v);
  }

  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) {
    int 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;
      ll c = a * b % MOD;
      ans += c * mod_pow(X, e.z) % MOD;
    } else {
      ll a = counter[e.y];
      ll b = N - a;
      ll c = a * b % MOD;
      ans += c * mod_pow(X, e.z) % MOD;
    }

    ans %= MOD;
  }

  cout << ans << endl;

  return 0;
}
0