結果

問題 No.1207 グラフX
ユーザー simansiman
提出日時 2023-03-15 20:27:52
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,667 bytes
コンパイル時間 1,693 ms
コンパイル使用メモリ 133,568 KB
実行使用メモリ 34,752 KB
最終ジャッジ日時 2023-10-18 12:31:02
合計ジャッジ時間 15,974 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 318 ms
24,920 KB
testcase_01 AC 318 ms
24,920 KB
testcase_02 AC 323 ms
24,920 KB
testcase_03 AC 320 ms
24,920 KB
testcase_04 AC 320 ms
24,920 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 278 ms
19,068 KB
testcase_09 AC 281 ms
22,960 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 269 ms
22,344 KB
testcase_13 AC 189 ms
13,004 KB
testcase_14 AC 312 ms
24,280 KB
testcase_15 AC 285 ms
22,872 KB
testcase_16 AC 176 ms
13,788 KB
testcase_17 AC 218 ms
18,012 KB
testcase_18 AC 176 ms
17,824 KB
testcase_19 AC 251 ms
17,064 KB
testcase_20 AC 311 ms
24,544 KB
testcase_21 AC 38 ms
9,664 KB
testcase_22 AC 217 ms
18,120 KB
testcase_23 AC 237 ms
22,080 KB
testcase_24 AC 152 ms
16,768 KB
testcase_25 AC 311 ms
24,544 KB
testcase_26 AC 253 ms
22,344 KB
testcase_27 AC 300 ms
23,488 KB
testcase_28 AC 285 ms
22,960 KB
testcase_29 AC 278 ms
23,224 KB
testcase_30 AC 160 ms
15,712 KB
testcase_31 AC 171 ms
12,872 KB
testcase_32 AC 126 ms
15,448 KB
testcase_33 AC 136 ms
15,712 KB
testcase_34 AC 271 ms
22,696 KB
testcase_35 AC 39 ms
9,664 KB
testcase_36 AC 266 ms
22,696 KB
testcase_37 AC 239 ms
18,804 KB
testcase_38 AC 68 ms
11,240 KB
testcase_39 AC 140 ms
15,976 KB
testcase_40 AC 147 ms
12,872 KB
testcase_41 AC 222 ms
16,692 KB
testcase_42 AC 4 ms
8,928 KB
testcase_43 AC 4 ms
8,932 KB
testcase_44 AC 4 ms
8,932 KB
testcase_45 AC 295 ms
24,984 KB
testcase_46 AC 295 ms
24,984 KB
testcase_47 AC 290 ms
24,984 KB
testcase_48 AC 294 ms
24,984 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, 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