結果

問題 No.1207 グラフX
ユーザー hanbei_dayohanbei_dayo
提出日時 2020-08-30 17:32:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 419 ms / 2,000 ms
コード長 2,686 bytes
コンパイル時間 1,443 ms
コンパイル使用メモリ 110,072 KB
実行使用メモリ 43,048 KB
最終ジャッジ日時 2023-08-09 17:18:36
合計ジャッジ時間 17,253 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 350 ms
22,796 KB
testcase_01 AC 351 ms
22,516 KB
testcase_02 AC 363 ms
22,580 KB
testcase_03 AC 358 ms
22,708 KB
testcase_04 AC 355 ms
23,140 KB
testcase_05 AC 400 ms
42,516 KB
testcase_06 AC 408 ms
43,048 KB
testcase_07 AC 419 ms
42,492 KB
testcase_08 AC 291 ms
18,144 KB
testcase_09 AC 308 ms
21,508 KB
testcase_10 AC 396 ms
34,028 KB
testcase_11 AC 405 ms
42,316 KB
testcase_12 AC 285 ms
18,568 KB
testcase_13 AC 174 ms
12,996 KB
testcase_14 AC 341 ms
22,476 KB
testcase_15 AC 293 ms
19,600 KB
testcase_16 AC 162 ms
13,492 KB
testcase_17 AC 232 ms
16,888 KB
testcase_18 AC 192 ms
16,076 KB
testcase_19 AC 237 ms
15,100 KB
testcase_20 AC 339 ms
22,620 KB
testcase_21 AC 33 ms
8,724 KB
testcase_22 AC 230 ms
16,876 KB
testcase_23 AC 251 ms
18,584 KB
testcase_24 AC 172 ms
15,188 KB
testcase_25 AC 346 ms
22,456 KB
testcase_26 AC 273 ms
18,752 KB
testcase_27 AC 330 ms
21,928 KB
testcase_28 AC 308 ms
22,476 KB
testcase_29 AC 310 ms
21,636 KB
testcase_30 AC 163 ms
13,468 KB
testcase_31 AC 151 ms
13,008 KB
testcase_32 AC 136 ms
13,296 KB
testcase_33 AC 142 ms
13,344 KB
testcase_34 AC 286 ms
20,372 KB
testcase_35 AC 34 ms
8,864 KB
testcase_36 AC 289 ms
21,408 KB
testcase_37 AC 246 ms
17,516 KB
testcase_38 AC 69 ms
10,508 KB
testcase_39 AC 151 ms
14,664 KB
testcase_40 AC 127 ms
12,544 KB
testcase_41 AC 221 ms
15,256 KB
testcase_42 AC 5 ms
8,060 KB
testcase_43 AC 5 ms
8,088 KB
testcase_44 AC 5 ms
8,044 KB
testcase_45 AC 327 ms
22,556 KB
testcase_46 AC 322 ms
22,676 KB
testcase_47 AC 320 ms
22,796 KB
testcase_48 AC 316 ms
22,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<functional>
#include<math.h>
#include<random>
#include <bitset>
using namespace std;
#define N (1000000000+7)
//#define N 998244353
#define INF 1e16
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> Q;
 
const int inf = (int)1e9; 
 
ll gcd(ll a, ll b) {
	if (b > a) {
		ll tmp = b;
		b = a;
		a = tmp;
	}
	if (a%b == 0)return b;
	else return gcd(b, a%b);
}
 
class UnionFind {
public:
	vector<int>par, rank, node;
	UnionFind(int n) :par(n), rank(n, 0),node(n,1) {
		for (int i = 0;i < n;i++)par[i] = i;
	}
	void init(int n) {
		for (int i = 0;i < n;i++) {
			par[i] = i;
			rank[i] = 0;
			node[i] = 1;
		}
	}
 
	int find(int x) {
		return par[x] == x ? x : par[x] = find(par[x]);
	}
 
	bool same(int x, int y) {
		return find(x) == find(y);
	}
 
	void unite_tree(int x, int y) {
		x = find(x);
		y = find(y);
		if (x == y)return;
		if (rank[x] < rank[y]) {
			par[x] = y;
		}
		else {
			par[y] = x;
			if (rank[x] == rank[y])rank[x]++;
		}
	}
	int root_size(int x){
		return rank[find(x)];
	}
 
	int size(int x) {
		return node[find(x)];
	}
 
	void unite_node(int x, int y) {
		x = find(x);
		y = find(y);
		if (x == y)return;
		if (size(x) < size(y))swap(x, y);
		node[x] += node[y];
		par[y] = par[x];
	}
};

ll x;
struct edge{
    int from;
    int to;
    ll cost;
};

struct E{
    int to;
    ll cost;
};

vector<E>g[200010];

ll inv(ll x,ll power) {
	ll res = 1;
	ll k = power;
	ll y = x%N;
	while (k) {
		if (k & 1)res = (res*y) % N;
		y = (y%N*y%N) % N;
		k /= 2;
	}
	return res;
}

ll dfs(int v, int p, ll n, ll &ans) {
     ll ret = 0;
     for (auto e: g[v]) {
         int to = e.to;
         ll cost = inv(x,e.cost);
         if (to == p) continue;
         ll a = dfs(to, v, n, ans);
         ll t = (((cost*a)%N)*(n-a))%N;
         ans = (ans+t)%N;
         ret = (ret+a)%N;
     }
     return (ret + 1)%N;
 }


int main(void){
    ll n,m;
    cin>>n>>m>>x;
    UnionFind uf = UnionFind(n);
    vector<edge>e;
    for(int i=0;i<m;i++){
        int a,b;
        ll z;
        cin>>a>>b>>z;
        a--;
        b--;
        e.push_back({a,b,z});
    }
    auto f = [&](edge &a,edge &b){
        return a.cost<b.cost;
    };
    sort(e.begin(),e.end(),f);
    for(int i=0;i<m;i++){
        int u = e[i].from;
        int v = e[i].to;
        ll c = e[i].cost;
        if(!uf.same(u,v)){
            uf.unite_node(u,v);
            g[u].push_back({v,c});
            g[v].push_back({u,c});
        }
    }
    ll ans = 0;
    dfs(0,-1,n,ans);
    cout<<ans<<endl;
	return 0;
}
0