結果

問題 No.1207 グラフX
ユーザー okok
提出日時 2020-08-30 14:35:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 244 ms / 2,000 ms
コード長 3,155 bytes
コンパイル時間 1,102 ms
コンパイル使用メモリ 97,384 KB
実行使用メモリ 46,224 KB
最終ジャッジ日時 2024-04-27 07:36:49
合計ジャッジ時間 10,360 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 183 ms
25,972 KB
testcase_01 AC 192 ms
26,136 KB
testcase_02 AC 173 ms
25,176 KB
testcase_03 AC 198 ms
25,364 KB
testcase_04 AC 192 ms
25,516 KB
testcase_05 AC 244 ms
45,184 KB
testcase_06 AC 236 ms
45,272 KB
testcase_07 AC 238 ms
45,264 KB
testcase_08 AC 138 ms
18,700 KB
testcase_09 AC 143 ms
23,736 KB
testcase_10 AC 225 ms
36,652 KB
testcase_11 AC 242 ms
46,224 KB
testcase_12 AC 140 ms
19,408 KB
testcase_13 AC 75 ms
10,472 KB
testcase_14 AC 174 ms
24,692 KB
testcase_15 AC 150 ms
20,340 KB
testcase_16 AC 71 ms
11,500 KB
testcase_17 AC 113 ms
17,432 KB
testcase_18 AC 90 ms
15,720 KB
testcase_19 AC 107 ms
14,416 KB
testcase_20 AC 180 ms
24,760 KB
testcase_21 AC 15 ms
6,940 KB
testcase_22 AC 135 ms
18,064 KB
testcase_23 AC 183 ms
19,580 KB
testcase_24 AC 79 ms
14,180 KB
testcase_25 AC 175 ms
24,684 KB
testcase_26 AC 138 ms
20,044 KB
testcase_27 AC 170 ms
24,208 KB
testcase_28 AC 146 ms
23,472 KB
testcase_29 AC 161 ms
23,556 KB
testcase_30 AC 74 ms
11,752 KB
testcase_31 AC 63 ms
10,092 KB
testcase_32 AC 67 ms
11,500 KB
testcase_33 AC 66 ms
12,028 KB
testcase_34 AC 150 ms
20,784 KB
testcase_35 AC 15 ms
6,944 KB
testcase_36 AC 151 ms
22,764 KB
testcase_37 AC 119 ms
17,936 KB
testcase_38 AC 31 ms
7,144 KB
testcase_39 AC 68 ms
13,420 KB
testcase_40 AC 50 ms
10,092 KB
testcase_41 AC 94 ms
14,168 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 2 ms
6,940 KB
testcase_44 AC 2 ms
6,944 KB
testcase_45 AC 149 ms
25,152 KB
testcase_46 AC 157 ms
25,152 KB
testcase_47 AC 166 ms
25,416 KB
testcase_48 AC 155 ms
25,152 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