結果

問題 No.1207 グラフX
ユーザー kk
提出日時 2020-10-09 14:42:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 250 ms / 2,000 ms
コード長 2,021 bytes
コンパイル時間 2,193 ms
コンパイル使用メモリ 205,564 KB
実行使用メモリ 33,288 KB
最終ジャッジ日時 2023-09-27 12:41:03
合計ジャッジ時間 15,443 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 166 ms
19,484 KB
testcase_01 AC 173 ms
19,528 KB
testcase_02 AC 166 ms
19,512 KB
testcase_03 AC 173 ms
19,816 KB
testcase_04 AC 174 ms
19,456 KB
testcase_05 AC 247 ms
33,136 KB
testcase_06 AC 244 ms
33,064 KB
testcase_07 AC 249 ms
33,048 KB
testcase_08 AC 135 ms
14,896 KB
testcase_09 AC 148 ms
17,740 KB
testcase_10 AC 235 ms
27,324 KB
testcase_11 AC 250 ms
33,288 KB
testcase_12 AC 131 ms
15,460 KB
testcase_13 AC 67 ms
10,172 KB
testcase_14 AC 167 ms
18,776 KB
testcase_15 AC 137 ms
16,384 KB
testcase_16 AC 68 ms
10,464 KB
testcase_17 AC 105 ms
14,460 KB
testcase_18 AC 89 ms
14,428 KB
testcase_19 AC 104 ms
12,104 KB
testcase_20 AC 171 ms
19,512 KB
testcase_21 AC 13 ms
8,152 KB
testcase_22 AC 106 ms
14,636 KB
testcase_23 AC 118 ms
15,356 KB
testcase_24 AC 77 ms
13,588 KB
testcase_25 AC 169 ms
18,944 KB
testcase_26 AC 126 ms
16,068 KB
testcase_27 AC 155 ms
17,820 KB
testcase_28 AC 142 ms
17,776 KB
testcase_29 AC 142 ms
17,832 KB
testcase_30 AC 71 ms
11,936 KB
testcase_31 AC 56 ms
9,392 KB
testcase_32 AC 60 ms
12,032 KB
testcase_33 AC 61 ms
12,000 KB
testcase_34 AC 139 ms
16,396 KB
testcase_35 AC 14 ms
8,252 KB
testcase_36 AC 133 ms
17,340 KB
testcase_37 AC 113 ms
14,972 KB
testcase_38 AC 31 ms
9,636 KB
testcase_39 AC 66 ms
12,956 KB
testcase_40 AC 42 ms
8,644 KB
testcase_41 AC 93 ms
12,588 KB
testcase_42 AC 4 ms
8,180 KB
testcase_43 AC 4 ms
8,248 KB
testcase_44 AC 4 ms
8,064 KB
testcase_45 AC 162 ms
19,572 KB
testcase_46 AC 160 ms
19,696 KB
testcase_47 AC 159 ms
19,404 KB
testcase_48 AC 162 ms
19,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,b,e) for (int i=(int)(b); i<(int)(e); i++)
#define ALL(x) (x).begin(), (x).end()

const double PI = acos(-1);

class UFT {
  int n;
  int cnt;           // number of connected components
  vector<int> par;
  vector<int> rank;
  vector<int> sz;    // size of each component
public:
  UFT(int n) : n(n), cnt(n), par(n), rank(n), sz(n) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
      sz[i] = 1;
    }
  }
  
  int find(int x) {
    return par[x] == x ? x : par[x] = find(par[x]);
  }
  
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    
    --cnt;
    if (rank[x] < rank[y]) {
      par[x] = y;
      sz[y] += sz[x];
    } else {
      par[y] = x;
      sz[x] += sz[y];
      if (rank[x] == rank[y])
        ++rank[x];
    }
    
  }
  
  bool same(int x, int y) {
    return find(x) == find(y);
  }
  
  int compCnt() {
    return cnt;
  }
  
  int size(int x) {
    return sz[find(x)];
  }
};

long long modpow(long long x, long long p, long long mod) {
  long long ret = 1;
  while (p) {
    if (p & 1)
      ret = ret * x % mod;
    x = x * x % mod;
    p >>= 1;
  }
  return ret;
}

const long long MOD = 1e9 + 7;
int n, m;
vector<pair<int, long long> > edges[200000];
long long ret = 0;


int dfs(int v, int par = -1) {
  int c = 1;
  for (auto &[u, dist]: edges[v]) {
    if (u == par) continue;
    int ch = dfs(u, v);
    c += ch;
    ret += dist * ch % MOD * (n - ch) % MOD;
    ret %= MOD;
  }
  return c;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  long long x;
  cin >> n >> m >> x;

  UFT tree(n);
  REP (i, m) {
    int u, v, z;
    cin >> u >> v >> z;
    --u, --v;
    if (tree.same(u, v))
      continue;
    else {
      tree.unite(u, v);
      long long dist = modpow(x, z, MOD);
      edges[u].emplace_back(v, dist);
      edges[v].emplace_back(u, dist);
    }
  }

  dfs(0);
  
  cout << ret << endl;
  
  return 0;
}
0