結果

問題 No.1207 グラフX
ユーザー leaf_1415
提出日時 2020-08-30 13:14:32
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 291 ms / 2,000 ms
コード長 2,243 bytes
コンパイル時間 939 ms
コンパイル使用メモリ 93,392 KB
実行使用メモリ 36,292 KB
最終ジャッジ日時 2024-11-15 06:30:54
合計ジャッジ時間 11,590 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define mod 1000000007
using namespace std;
typedef pair<llint, llint> P;
typedef pair<llint, P> E;
struct UnionFind{
int size;
vector<int> parent;
UnionFind(){}
UnionFind(int size){
this->size = size;
parent.resize(size+1);
init();
}
void init(){
for(int i = 0; i <= size; i++) parent[i] = i;
}
int root(int i){
if(parent[i] == i) return i;
return parent[i] = root(parent[i]);
}
bool same(int i, int j){
return root(i) == root(j);
}
void unite(int i, int j){
int root_i = root(i), root_j = root(j);
if(root_i == root_j) return;
parent[root_i] = root_j;
}
};
struct edge{
llint to, cost;
edge(){}
edge(llint a, llint b){
to = a, cost = b;
}
};
llint modpow(llint a, llint n)
{
if(n == 0) return 1;
if(n % 2){
return ((a%mod) * (modpow(a, n-1)%mod)) % mod;
}
else{
return modpow((a*a)%mod, n/2) % mod;
}
}
llint n, m, x;
vector<edge> G[200005];
vector<E> vec;
UnionFind uf(200005);
llint ans = 0;
llint dfs(int v, int p)
{
llint ret = 1;
for(int i = 0; i < G[v].size(); i++){
if(G[v][i].to == p) continue;
llint res = dfs(G[v][i].to, v);
ans += res * (n-res) % mod * modpow(x, G[v][i].cost) % mod;
ans %= mod;
ret += res;
}
return ret;
}
int main(void)
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> x;
llint u, v, w;
for(int i = 1; i <= m; i++){
cin >> u >> v >> w;
vec.push_back(E(w, P(u, v)));
}
sort(vec.begin(), vec.end());
for(int i = 0; i < vec.size(); i++){
llint w = vec[i].first, u = vec[i].second.first, v = vec[i].second.second;
if(uf.same(u, v)) continue;
uf.unite(u, v);
G[u].push_back(edge(v, w));
G[v].push_back(edge(u, w));
}
dfs(1, -1);
cout << ans << endl;
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0