結果

問題 No.1207 グラフX
ユーザー SSRSSSRS
提出日時 2020-08-30 13:17:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 391 ms / 2,000 ms
コード長 1,830 bytes
コンパイル時間 2,544 ms
コンパイル使用メモリ 218,020 KB
実行使用メモリ 45,308 KB
最終ジャッジ日時 2024-04-27 06:48:33
合計ジャッジ時間 16,958 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 305 ms
30,652 KB
testcase_01 AC 312 ms
30,572 KB
testcase_02 AC 308 ms
30,664 KB
testcase_03 AC 318 ms
30,728 KB
testcase_04 AC 310 ms
30,616 KB
testcase_05 AC 390 ms
45,256 KB
testcase_06 AC 391 ms
45,104 KB
testcase_07 AC 378 ms
45,048 KB
testcase_08 AC 275 ms
21,100 KB
testcase_09 AC 277 ms
24,852 KB
testcase_10 AC 384 ms
40,908 KB
testcase_11 AC 390 ms
45,308 KB
testcase_12 AC 272 ms
22,092 KB
testcase_13 AC 176 ms
9,600 KB
testcase_14 AC 294 ms
29,708 KB
testcase_15 AC 269 ms
24,028 KB
testcase_16 AC 165 ms
10,880 KB
testcase_17 AC 214 ms
18,932 KB
testcase_18 AC 167 ms
18,856 KB
testcase_19 AC 239 ms
15,104 KB
testcase_20 AC 302 ms
30,252 KB
testcase_21 AC 32 ms
6,944 KB
testcase_22 AC 211 ms
19,316 KB
testcase_23 AC 231 ms
21,476 KB
testcase_24 AC 147 ms
17,020 KB
testcase_25 AC 306 ms
29,836 KB
testcase_26 AC 235 ms
23,072 KB
testcase_27 AC 282 ms
27,208 KB
testcase_28 AC 271 ms
24,972 KB
testcase_29 AC 264 ms
26,776 KB
testcase_30 AC 155 ms
13,324 KB
testcase_31 AC 156 ms
7,936 KB
testcase_32 AC 118 ms
13,220 KB
testcase_33 AC 131 ms
13,316 KB
testcase_34 AC 264 ms
23,568 KB
testcase_35 AC 34 ms
6,944 KB
testcase_36 AC 258 ms
24,652 KB
testcase_37 AC 239 ms
20,268 KB
testcase_38 AC 68 ms
7,680 KB
testcase_39 AC 136 ms
14,152 KB
testcase_40 AC 128 ms
6,940 KB
testcase_41 AC 210 ms
15,152 KB
testcase_42 AC 2 ms
6,944 KB
testcase_43 AC 2 ms
6,944 KB
testcase_44 AC 2 ms
6,944 KB
testcase_45 AC 276 ms
30,968 KB
testcase_46 AC 270 ms
30,848 KB
testcase_47 AC 271 ms
30,820 KB
testcase_48 AC 281 ms
30,860 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