結果

問題 No.1207 グラフX
ユーザー hanbei_dayohanbei_dayo
提出日時 2020-08-30 17:32:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 351 ms / 2,000 ms
コード長 2,686 bytes
コンパイル時間 1,001 ms
コンパイル使用メモリ 111,096 KB
実行使用メモリ 42,680 KB
最終ジャッジ日時 2024-04-27 12:06:23
合計ジャッジ時間 14,523 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 296 ms
23,756 KB
testcase_01 AC 291 ms
22,904 KB
testcase_02 AC 301 ms
22,888 KB
testcase_03 AC 304 ms
22,880 KB
testcase_04 AC 296 ms
22,752 KB
testcase_05 AC 351 ms
42,516 KB
testcase_06 AC 343 ms
42,520 KB
testcase_07 AC 348 ms
42,672 KB
testcase_08 AC 252 ms
18,508 KB
testcase_09 AC 250 ms
21,860 KB
testcase_10 AC 329 ms
34,140 KB
testcase_11 AC 344 ms
42,680 KB
testcase_12 AC 242 ms
18,796 KB
testcase_13 AC 179 ms
13,216 KB
testcase_14 AC 310 ms
22,736 KB
testcase_15 AC 280 ms
19,712 KB
testcase_16 AC 158 ms
13,732 KB
testcase_17 AC 210 ms
18,304 KB
testcase_18 AC 164 ms
16,336 KB
testcase_19 AC 217 ms
15,444 KB
testcase_20 AC 290 ms
22,564 KB
testcase_21 AC 30 ms
9,024 KB
testcase_22 AC 197 ms
17,612 KB
testcase_23 AC 214 ms
19,596 KB
testcase_24 AC 141 ms
15,412 KB
testcase_25 AC 288 ms
22,448 KB
testcase_26 AC 230 ms
19,740 KB
testcase_27 AC 276 ms
22,124 KB
testcase_28 AC 262 ms
21,856 KB
testcase_29 AC 258 ms
22,432 KB
testcase_30 AC 140 ms
13,876 KB
testcase_31 AC 139 ms
13,212 KB
testcase_32 AC 113 ms
13,556 KB
testcase_33 AC 120 ms
13,520 KB
testcase_34 AC 252 ms
20,016 KB
testcase_35 AC 31 ms
9,160 KB
testcase_36 AC 240 ms
21,708 KB
testcase_37 AC 218 ms
18,272 KB
testcase_38 AC 62 ms
10,608 KB
testcase_39 AC 131 ms
14,896 KB
testcase_40 AC 121 ms
12,380 KB
testcase_41 AC 190 ms
16,176 KB
testcase_42 AC 4 ms
8,140 KB
testcase_43 AC 3 ms
8,120 KB
testcase_44 AC 3 ms
7,996 KB
testcase_45 AC 278 ms
23,488 KB
testcase_46 AC 266 ms
22,916 KB
testcase_47 AC 269 ms
22,792 KB
testcase_48 AC 266 ms
22,796 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