結果

問題 No.1207 グラフX
ユーザー simansiman
提出日時 2023-03-15 20:51:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,610 bytes
コンパイル時間 1,101 ms
コンパイル使用メモリ 105,248 KB
実行使用メモリ 34,776 KB
最終ジャッジ日時 2023-10-18 12:32:23
合計ジャッジ時間 15,567 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 305 ms
25,008 KB
testcase_01 AC 308 ms
25,008 KB
testcase_02 AC 302 ms
25,008 KB
testcase_03 AC 304 ms
25,008 KB
testcase_04 AC 303 ms
25,008 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 262 ms
19,728 KB
testcase_09 AC 265 ms
23,424 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 255 ms
23,160 KB
testcase_13 AC 181 ms
13,928 KB
testcase_14 AC 303 ms
24,480 KB
testcase_15 AC 271 ms
23,424 KB
testcase_16 AC 168 ms
14,516 KB
testcase_17 AC 210 ms
18,740 KB
testcase_18 AC 166 ms
17,956 KB
testcase_19 AC 238 ms
17,420 KB
testcase_20 AC 298 ms
24,744 KB
testcase_21 AC 37 ms
10,060 KB
testcase_22 AC 213 ms
18,740 KB
testcase_23 AC 225 ms
22,896 KB
testcase_24 AC 146 ms
17,164 KB
testcase_25 AC 298 ms
24,744 KB
testcase_26 AC 241 ms
23,160 KB
testcase_27 AC 284 ms
23,688 KB
testcase_28 AC 274 ms
23,424 KB
testcase_29 AC 269 ms
23,424 KB
testcase_30 AC 152 ms
16,108 KB
testcase_31 AC 164 ms
13,928 KB
testcase_32 AC 120 ms
15,844 KB
testcase_33 AC 132 ms
16,108 KB
testcase_34 AC 260 ms
23,160 KB
testcase_35 AC 38 ms
10,060 KB
testcase_36 AC 254 ms
23,160 KB
testcase_37 AC 230 ms
19,004 KB
testcase_38 AC 65 ms
11,636 KB
testcase_39 AC 135 ms
16,108 KB
testcase_40 AC 141 ms
13,928 KB
testcase_41 AC 214 ms
17,156 KB
testcase_42 AC 4 ms
9,336 KB
testcase_43 AC 4 ms
9,340 KB
testcase_44 AC 5 ms
9,340 KB
testcase_45 AC 284 ms
25,272 KB
testcase_46 AC 281 ms
25,272 KB
testcase_47 AC 288 ms
25,272 KB
testcase_48 AC 284 ms
25,272 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];
bool visited[MAX_N];

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

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

    cnt += dfs(u);
  }

  counter[v] = cnt;

  return cnt;
}

int main() {
  memset(visited, false, sizeof(visited));
  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