結果

問題 No.1207 グラフX
ユーザー okok
提出日時 2020-08-30 14:35:14
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 250 ms / 2,000 ms
コード長 3,155 bytes
コンパイル時間 1,444 ms
コンパイル使用メモリ 95,292 KB
実行使用メモリ 45,532 KB
最終ジャッジ日時 2023-08-09 12:33:18
合計ジャッジ時間 10,851 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 189 ms
26,288 KB
testcase_01 AC 178 ms
24,944 KB
testcase_02 AC 187 ms
25,476 KB
testcase_03 AC 182 ms
25,820 KB
testcase_04 AC 185 ms
25,488 KB
testcase_05 AC 241 ms
44,952 KB
testcase_06 AC 250 ms
44,744 KB
testcase_07 AC 242 ms
45,128 KB
testcase_08 AC 140 ms
18,740 KB
testcase_09 AC 145 ms
22,924 KB
testcase_10 AC 224 ms
36,304 KB
testcase_11 AC 249 ms
45,532 KB
testcase_12 AC 136 ms
18,712 KB
testcase_13 AC 73 ms
10,828 KB
testcase_14 AC 160 ms
24,776 KB
testcase_15 AC 143 ms
20,832 KB
testcase_16 AC 72 ms
10,884 KB
testcase_17 AC 111 ms
17,068 KB
testcase_18 AC 87 ms
15,524 KB
testcase_19 AC 103 ms
13,760 KB
testcase_20 AC 165 ms
25,720 KB
testcase_21 AC 14 ms
4,760 KB
testcase_22 AC 104 ms
16,644 KB
testcase_23 AC 119 ms
17,956 KB
testcase_24 AC 79 ms
14,096 KB
testcase_25 AC 175 ms
24,748 KB
testcase_26 AC 125 ms
19,472 KB
testcase_27 AC 159 ms
23,548 KB
testcase_28 AC 151 ms
22,828 KB
testcase_29 AC 147 ms
22,980 KB
testcase_30 AC 71 ms
11,708 KB
testcase_31 AC 64 ms
10,200 KB
testcase_32 AC 59 ms
11,252 KB
testcase_33 AC 63 ms
11,764 KB
testcase_34 AC 132 ms
21,524 KB
testcase_35 AC 15 ms
4,812 KB
testcase_36 AC 137 ms
22,968 KB
testcase_37 AC 119 ms
18,532 KB
testcase_38 AC 32 ms
6,900 KB
testcase_39 AC 72 ms
12,900 KB
testcase_40 AC 52 ms
9,864 KB
testcase_41 AC 101 ms
14,468 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 1 ms
4,380 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 165 ms
25,904 KB
testcase_46 AC 168 ms
25,916 KB
testcase_47 AC 173 ms
25,792 KB
testcase_48 AC 174 ms
25,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007; 

struct fast_io {
	fast_io(){
		std::cin.tie(nullptr);
		std::ios::sync_with_stdio(false);
	};
} fio;


class union_find
{
	int  _setnum;
	vector<int> par, nume;
public:
	union_find(){
	}
	
	union_find(int x){
		par.resize(x);
		nume.resize(x);
		init();
	}
	
	~union_find(){
		//
		
	}
	
	void clear(){
		_setnum = 0;
		par.clear();
		nume.clear();
	}
	
	void init(){
		_setnum = par.size();
		for(int i = 0; i < par.size(); i++){
			par[i] = i;
			nume[i] = 1;
		}
	}
	
	void resize(int x){
		
		par.resize(x);
		nume.resize(x);
		init();
	}

	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;
		
		_setnum--;
		
		if(nume[x] > nume[y]) std::swap(x,y);
		
		par[x] = y;
		nume[y] += nume[x];
	}
	
	bool same(int x, int y){
		return find(x) == find(y);
	}
	
	int numel(int x){
		return nume[find(x)];
	}
	
	int size(){
		return par.size();
	}
	
	int setnum(){
		return _setnum;
	}
};

vector<vector<pair<int,int>>>  kruskal(vector<pair<int,pair<int,int>>>& edge, int V){
	union_find uf(V);
	int res = 0, count = 0;
	vector<vector<pair<int,int>>> tree(V);
    
	sort(edge.begin(), edge.end());
	
	for(int i = 0; i < edge.size(); i++){
		if(!uf.same(edge[i].second.first, edge[i].second.second)){
			uf.unite(edge[i].second.first, edge[i].second.second);
			res += edge[i].first;
            tree[edge[i].second.first].push_back({edge[i].second.second, edge[i].first});
            tree[edge[i].second.second].push_back({edge[i].second.first, edge[i].first});
			count++;
		}
	}
	return tree;
	// if(count == V-1) return res;
	// return -1;
}


long long power(long long x, long long n){
    long long ans = 1;
    for(;n;n >>= 1, x *= x, ans %= MOD, x %= MOD)
        if(n&1)ans*=x;
    return ans % MOD;
}

int ans = 0;
int X;

int solve(vector<vector<pair<int,int>>> &tree, int cur, int per){
    int sum = 1;
    
    for(pair<int,int> nex : tree[cur]){
        
        if(nex.first == per) continue;
        
        int temp = solve(tree, nex.first, cur);
        
        sum += temp;
        
        // cout<<"cur = "<<cur<<" nex = "<<nex.first<<" nex.s = "<<nex.second<<" temp = "<<temp<<" tree "<<tree.size()<<endl;
        // cout<<power(X, nex.second) * temp % MOD * ((int)tree.size() - temp) <<endl;
        ans += power(X, nex.second) * temp % MOD * ((int)tree.size() - temp) % MOD;
        ans %= MOD;
    }
    
    return sum;
}

signed main(){
	cout<<fixed<<setprecision(10);
	
    int N, M;
	vector<pair<int,pair<int,int>>> edge;
	vector<vector<pair<int,int>>> tree;
    
    cin>>N>>M>>X;
    
    for(int i = 0; i < M; i++){
        int x, y, z;
        
        cin>>x>>y>>z;
        
        x--, y--;
        
        edge.push_back({z, {x, y}});
    }
	
    tree = kruskal(edge, N);
    
    solve(tree, 0, -1);
    
    cout<<ans<<endl;
    
	return 0;
}
0