結果
| 問題 |
No.1207 グラフX
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-08-30 14:35:14 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 246 ms / 2,000 ms |
| コード長 | 3,155 bytes |
| コンパイル時間 | 1,255 ms |
| コンパイル使用メモリ | 97,088 KB |
| 実行使用メモリ | 44,908 KB |
| 最終ジャッジ日時 | 2024-11-15 07:44:26 |
| 合計ジャッジ時間 | 10,309 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 46 |
ソースコード
#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;
}