結果

問題 No.1207 グラフX
ユーザー SSRSSSRS
提出日時 2020-08-30 13:17:14
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 367 ms / 2,000 ms
コード長 1,830 bytes
コンパイル時間 3,274 ms
コンパイル使用メモリ 215,252 KB
実行使用メモリ 45,188 KB
最終ジャッジ日時 2023-08-09 11:41:37
合計ジャッジ時間 16,023 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 289 ms
30,436 KB
testcase_01 AC 290 ms
30,540 KB
testcase_02 AC 277 ms
30,296 KB
testcase_03 AC 277 ms
30,496 KB
testcase_04 AC 272 ms
30,616 KB
testcase_05 AC 348 ms
44,964 KB
testcase_06 AC 362 ms
44,968 KB
testcase_07 AC 354 ms
45,176 KB
testcase_08 AC 246 ms
20,848 KB
testcase_09 AC 245 ms
24,676 KB
testcase_10 AC 352 ms
40,656 KB
testcase_11 AC 367 ms
45,188 KB
testcase_12 AC 245 ms
21,888 KB
testcase_13 AC 161 ms
9,664 KB
testcase_14 AC 284 ms
29,312 KB
testcase_15 AC 250 ms
23,988 KB
testcase_16 AC 149 ms
10,944 KB
testcase_17 AC 200 ms
18,892 KB
testcase_18 AC 156 ms
18,592 KB
testcase_19 AC 218 ms
14,964 KB
testcase_20 AC 284 ms
29,844 KB
testcase_21 AC 29 ms
4,380 KB
testcase_22 AC 189 ms
18,968 KB
testcase_23 AC 209 ms
21,224 KB
testcase_24 AC 136 ms
16,860 KB
testcase_25 AC 283 ms
29,588 KB
testcase_26 AC 226 ms
22,964 KB
testcase_27 AC 267 ms
27,016 KB
testcase_28 AC 263 ms
24,960 KB
testcase_29 AC 250 ms
26,540 KB
testcase_30 AC 141 ms
13,208 KB
testcase_31 AC 140 ms
7,840 KB
testcase_32 AC 108 ms
12,964 KB
testcase_33 AC 121 ms
13,104 KB
testcase_34 AC 248 ms
23,512 KB
testcase_35 AC 30 ms
4,376 KB
testcase_36 AC 240 ms
24,388 KB
testcase_37 AC 217 ms
20,012 KB
testcase_38 AC 59 ms
7,300 KB
testcase_39 AC 124 ms
14,152 KB
testcase_40 AC 117 ms
5,712 KB
testcase_41 AC 199 ms
15,036 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 1 ms
4,380 KB
testcase_44 AC 1 ms
4,376 KB
testcase_45 AC 272 ms
30,832 KB
testcase_46 AC 267 ms
31,052 KB
testcase_47 AC 261 ms
30,760 KB
testcase_48 AC 265 ms
30,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
struct unionfind{
	vector<int> p;
	vector<int> sz;
	unionfind(int N): p(vector<int>(N, -1)), sz(vector<int>(N, 1)){
	};
	int root(int x){
		if (p[x] == -1){
			return x;
		} else {
			p[x] = root(p[x]);
			return p[x];
		}
	}
	bool same(int x, int y){
		return root(x) == root(y);
	}
	void unite(int x, int y){
		x = root(x);
		y = root(y);
		if (x != y){
			p[x] = y;
			sz[y] += sz[x];
		}
	}
	int size(int x){
	  return sz[root(x)];
	}
};
long long modpow(long long a, long long b){
	long long ans = 1;
	while (b > 0){
		if (b % 2 == 1){
			ans *= a;
			ans %= MOD;
		}
		a *= a;
		a %= MOD;
		b /= 2;
	}
	return ans;
}
int dfs(vector<vector<int>> &c, vector<int> &dp, int v = 0){
  for (int w : c[v]){
    dp[v] += dfs(c, dp, w);
  }
  return dp[v];
}
int main(){
  int N, M, X;
  cin >> N >> M >> X;
  vector<int> x(M), y(M), z(M);
  for (int i = 0; i < M; i++){
    cin >> x[i] >> y[i] >> z[i];
    x[i]--;
    y[i]--;
  }
  unionfind UF(N);
  vector<vector<pair<long long, int>>> E(N);
  for (int i = 0; i < M; i++){
    if (!UF.same(x[i], y[i])){
      UF.unite(x[i], y[i]);
      long long c = modpow(X, z[i]);
      E[x[i]].push_back(make_pair(c, y[i]));
      E[y[i]].push_back(make_pair(c, x[i]));
    }
  }
  vector<int> p(N, -1);
  vector<long long> d(N, 0);
  vector<vector<int>> c(N);
  queue<int> Q;
  Q.push(0);
  while (!Q.empty()){
    int v = Q.front();
    Q.pop();
    for (auto P : E[v]){
      int w = P.second;
      if (w != p[v]){
        p[w] = v;
        d[w] = P.first;
        c[v].push_back(w);
        Q.push(w);
      }
    }
  }
  vector<int> dp(N, 1);
  dfs(c, dp);
  long long ans = 0;
  for (int i = 1; i < N; i++){
    ans += d[i] * dp[i] % MOD * (N - dp[i]) % MOD;
    ans %= MOD;
  }
  cout << ans << endl;
}
0